我使用简单的日期格式将字符串转换为日期,但是日期的月份总是返回JANUARY,这是我的代码:
public static void main(String args[]){
String date = "2017-12-29";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, YYYY");
try {
Date startEntryDate = formatter.parse(date);
System.out.println(dateFormatter.format(startEntryDate));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:2)
如上所述,您必须使用MM
代替mm
,但是?
为什么不使用java.time
库:
String month = LocalDate.parse("2017-12-29").getMonth().name();
答案 1 :(得分:1)
尝试使用
new SimpleDateFormat("yyyy-MM-dd");
mm
表示分钟。
MM
- 几个月。
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
答案 2 :(得分:1)
试试这个:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, yyyy");
答案 3 :(得分:0)
Try this:
new SimpleDateFormat("MM/dd/yyyy")
MM is "month" (not mm)
dd is "day" (not DD)
It's all in the javadoc for SimpleDateFormat
FYI, the reason your format is still a valid date format is that:
mm is "minutes"
DD is "day in year"
Also, you don't need the cast to Date... it already is a Date (or it
explodes):
public static void main(String[] args) throws ParseException {
System.out.println(new
SimpleDateFormat("MM/dd/yyyy").parse("08/16/2011"));
}
Output:
8月16日星期二00:00:00 EST 2011
答案 4 :(得分:0)
试试这个:
String date = "2017-8-28";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, YYYY");
try {
Date startEntryDate = formatter.parse(date);
System.out.println(dateFormatter.format(startEntryDate));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}