JAVA:Java 中当前日期的自定义格式?
JAVA: Custom Format for Current Date in Java?
我想使用简单日期格式实现以下格式:
2016 年 7 月 13 日
我使用了下面的代码,但我不知道要使用的格式:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
Date currentDate = new Date();
String currentDateString = dateFormat.format(currentDate);
try {
currentDate = dateFormat.parse(currentDateString);
} catch (ParseException e) {
}
请指导。
你需要解析"MMMM dd,yyyy"
DateFormat dateFormat = new SimpleDateFormat("MMMM dd,yyyy");
Date currentDate = new Date();
String currentDateString = dateFormat.format(currentDate);
System.out.println(currentDateString);
try {
currentDate = dateFormat.parse(currentDateString);
} catch (ParseException e) {
}
}
MMM - 一年中的月份作为文本
dd - 以 numner
表示的月份
yyyy - 以数字
表示的年份
因此:
MMM dd, yyyy
我没试过,但应该可以,如果有效请给我一个简短的反馈。
您需要使用日期格式'MMMM dd, yyyy'
示例代码:
package problems.outputDateAsFormatX;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateOutput {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy");
Date currentDate = new Date();
String currentDateString = dateFormat.format(currentDate);
System.out.println("My date is: " + currentDateString);
}
}
输出:
My date is: July 04, 2016
格式源自 SimpleDateFormat 文档。
"dd" 和 "yyyy" 位非常标准,您可能需要特别注意月份位。
Month: If the number of pattern letters is 3 or more, the month is
interpreted as text; otherwise, it is interpreted as a number.
所以 'MMM' 会给你缩写 "Jul",添加一个额外的 "M" 解析为 July。
我想使用简单日期格式实现以下格式:
2016 年 7 月 13 日
我使用了下面的代码,但我不知道要使用的格式:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
Date currentDate = new Date();
String currentDateString = dateFormat.format(currentDate);
try {
currentDate = dateFormat.parse(currentDateString);
} catch (ParseException e) {
}
请指导。
你需要解析"MMMM dd,yyyy"
DateFormat dateFormat = new SimpleDateFormat("MMMM dd,yyyy");
Date currentDate = new Date();
String currentDateString = dateFormat.format(currentDate);
System.out.println(currentDateString);
try {
currentDate = dateFormat.parse(currentDateString);
} catch (ParseException e) {
}
}
MMM - 一年中的月份作为文本
dd - 以 numner
表示的月份
yyyy - 以数字
因此:
MMM dd, yyyy
我没试过,但应该可以,如果有效请给我一个简短的反馈。
您需要使用日期格式'MMMM dd, yyyy'
示例代码:
package problems.outputDateAsFormatX;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateOutput {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy");
Date currentDate = new Date();
String currentDateString = dateFormat.format(currentDate);
System.out.println("My date is: " + currentDateString);
}
}
输出:
My date is: July 04, 2016
格式源自 SimpleDateFormat 文档。
"dd" 和 "yyyy" 位非常标准,您可能需要特别注意月份位。
Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
所以 'MMM' 会给你缩写 "Jul",添加一个额外的 "M" 解析为 July。