[英]How can I format a Date in Java as only month, day and hour?
如果僅需要打印日期(MMM),日期(DD)和小時(HH),該如何格式化日期?
所以輸出看起來像:
Jul 18 9
(即7月18日09:00)。
我嘗試了以下
private static void createDate () {
String startConcat = startMonth + " " + startDate + " " + startTime;
DateFormat start = new SimpleDateFormat ("MMM DD H");
try {
Date date = (Date)start.parse(startConcat);
System.out.println(date);
} catch (ParseException e) {
}
}
而且似乎也無法正確閱讀我的月份...我將其作為輸出
Sun Jan 18 09:00:00 EST 1970
任何幫助將不勝感激。
您應該使用format
( javadoc )方法。
private static void createDate () throws Exception {
String startConcat = startMonth + " " + startDate + " " + startTime;
DateFormat formatter = new SimpleDateFormat ("MMM DD H");
Date date = (Date) formatter.parse(startConcat);
System.out.println(formatter.format(date));
}
您正在定義用於解析日期的模式,而不是打印日期。 您還需要將模式用於輸出;
System.out.println(start.format(date));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.