JAVA:JAVA中当前日期的自定义格式?
我想使用简单的日期格式实现以下格式: 2016年7月13日 我使用了以下代码,但我不知道要使用的格式:JAVA:JAVA中当前日期的自定义格式?,java,date,simpledateformat,Java,Date,Simpledateformat,我想使用简单的日期格式实现以下格式: 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.par
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”
MMM-以文本形式表示一年中的月份
dd-作为numner的月日
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);
}
}
输出:
我的日期是:2016年7月4日
格式来源于文档
“dd”和“yyy”位是相当标准的,您可能需要特别注意月份位
月份:如果图案字母数为3个或更多,则月份为
解释为文本;否则,它将被解释为一个数字
因此,“MMM”会给你一个缩写“Jul”,在July中添加一个额外的“M”解析,阅读SimpleDataFormat的javadoc,找出isI没有找到的格式。。这行得通吗:“mmmdd,'yyyy”@Shirish您应该使用mmmdd,yyyyysee的API文档(如果您希望一个具有特定格式的
Date
对象,那么这是行不通的。Date
对象没有格式。您无法获得一个自己知道以特定格式打印的Date
对象;Date
对象不记得它的格式。@downvorter注释问题已解决。)格式化日期,而不是解析日期。格式化=将日期转换为字符串,解析=将字符串转换为日期。@Jesper我不知道你是否是投票人,但我的答案与接受的答案有何不同
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);
}
}