给你个例子: String myString = "2005/12/20";SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA); Date d = sdf.parse(myString);
你的string可以按如下格式转成时间 String myString = "20060823143437";SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA); Date d = sdf.parse(myString);转成时间后,再用SimpleDateFormat转成2006-08-23 14:34:37 匹配的时间格式:yyyy-MM-dd HH:mm:ss
public static void main(String[] args) throws Exception { String s = "20060823143437"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); Date d = sdf.parse(s); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String ss = sdf2.format(d); System.out.println(ss); }
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = formatter.parse("20060823143437"); SimpleDateFormat formatternext = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); formatternext.parse(formatter.format(date));
public static void main(String[] args) throws Exception { String s = "20060823143437"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); Date d = sdf.parse(s); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String ss = sdf2.format(d); System.out.println(ss); } // 这个ss最后转换不还是 String 型吗? 我要的是(date)日期型的这样格式:2006-08-23 14:34:37能否在具体点!谢谢! 小弟刚学习,请见亮!
日期型的数据只保存日期的信息,格式信息不保存 因此,如果你只需要日期型数据,则第一次转换结束的时候已经可以了String myString = "20060823143437"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA); Date d = sdf.parse(myString);等需要输出的时候,再用后面的代码将其输出 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String ss = sdf2.format(d); System.out.println(ss);
如果不行可以这样试试,就是有点麻烦 public Date getDate(String time) { Calender c = Calender.getInstence(); c.set(Integer.parseInt(time.substring(0,3)), Integer.parseInt(time.substring(4,5)),Integer.parseInt(time.substring(6,7)), Integer.parseInt(time.substring(8,9)),Integer.parseInt( time.substring(10,11)), Integer.parseInt(time.substring(12,13))) ; return c.getTime(); }
String myString = "20060823143437"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA); Date d = sdf.parse(myString);java.sql.Date sd = new java.sql.Date(d.getTime());
String myString = "2005/12/20";SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
Date d = sdf.parse(myString);
String myString = "20060823143437";SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
Date d = sdf.parse(myString);转成时间后,再用SimpleDateFormat转成2006-08-23 14:34:37
匹配的时间格式:yyyy-MM-dd HH:mm:ss
String s = "20060823143437";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date d = sdf.parse(s);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ss = sdf2.format(d);
System.out.println(ss);
}
Date date = formatter.parse("20060823143437");
SimpleDateFormat formatternext = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatternext.parse(formatter.format(date));
String s = "20060823143437";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date d = sdf.parse(s);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ss = sdf2.format(d);
System.out.println(ss);
}
//
这个ss最后转换不还是 String 型吗?
我要的是(date)日期型的这样格式:2006-08-23 14:34:37能否在具体点!谢谢!
小弟刚学习,请见亮!
因此,如果你只需要日期型数据,则第一次转换结束的时候已经可以了String myString = "20060823143437";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
Date d = sdf.parse(myString);等需要输出的时候,再用后面的代码将其输出
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ss = sdf2.format(d);
System.out.println(ss);
public Date getDate(String time) {
Calender c = Calender.getInstence();
c.set(Integer.parseInt(time.substring(0,3)), Integer.parseInt(time.substring(4,5)),Integer.parseInt(time.substring(6,7)), Integer.parseInt(time.substring(8,9)),Integer.parseInt( time.substring(10,11)), Integer.parseInt(time.substring(12,13))) ;
return c.getTime();
}
转化来的是java.util.Date中的类型,那我怎么把他从java.util.Date转化到java.sql.Date的类型呀!最好详细点的例子!
谢谢
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
Date d = sdf.parse(myString);java.sql.Date sd = new java.sql.Date(d.getTime());