什么是最好的方法转换格式的字符串' 1月2日,2010年'到一个日期在Java?
最后,我希望将月、日和年分解为整数,以便我可以使用
Date date = new Date();
date.setMonth()..
date.setYear()..
date.setDay()..
date.setlong currentTime = date.getTime();
将日期转换为时间。
什么是最好的方法转换格式的字符串' 1月2日,2010年'到一个日期在Java?
最后,我希望将月、日和年分解为整数,以便我可以使用
Date date = new Date();
date.setMonth()..
date.setYear()..
date.setDay()..
date.setlong currentTime = date.getTime();
将日期转换为时间。
当前回答
String str_date = "11-June-07";
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date date = formatter.parse(str_date);
其他回答
您可以使用SimpleDateformat更改日期字符串
sdf = new SimpleDateFormat("yyyy-MM-dd"); String strDate = "2000-01-01"; Date Date = sdf.parse(strDate);
String str_date = "11-June-07";
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date date = formatter.parse(str_date);
在处理SimpleDateFormat类时,重要的是要记住Date不是线程安全的,不能与多个线程共享单个Date对象。
“m”和“m”之间也有很大的区别,小写字母用于表示分钟,大写字母用于表示月。“d”和“d”也是一样。这可能会导致经常被忽视的细微错误。有关更多细节,请参阅Javadoc或Java中字符串转换为日期的指南。
我们使用了两个简单的格式化程序:
我们想要哪种格式的日期? 实际显示的是哪种格式日期?
我们解析完整的日期到时间格式:
date="2016-05-06 16:40:32";
public static String setDateParsing(String date) throws ParseException {
// This is the format date we want
DateFormat mSDF = new SimpleDateFormat("hh:mm a");
// This format date is actually present
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd hh:mm");
return mSDF.format(formatter.parse(date));
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date1 = null;
Date date2 = null;
try {
date1 = dateFormat.parse(t1);
date2 = dateFormat.parse(t2);
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String StDate = formatter.format(date1);
String edDate = formatter.format(date2);
System.out.println("ST "+ StDate);
System.out.println("ED "+ edDate);