我有这个代码片段:
DateFormat formatter1;
formatter1 = new SimpleDateFormat("mm/DD/yyyy");
System.out.println((Date)formatter1.parse("08/16/2011"));
当我运行这段代码时,我得到以下输出:
Sun Jan 16 00:10:00 IST 2011
我期望:
Tue Aug 16 "Whatever Time" IST 2011
我的意思是说,我没有得到预期的月份。错误出在哪里?
发布于 2013-12-03 17:56:36
M- min M-月
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
发布于 2013-02-05 18:02:53
使用下面的函数
/**
* Format a time from a given format to given target format
*
* @param inputFormat
* @param inputTimeStamp
* @param outputFormat
* @return
* @throws ParseException
*/
private static String TimeStampConverter(final String inputFormat,
String inputTimeStamp, final String outputFormat)
throws ParseException {
return new SimpleDateFormat(outputFormat).format(new SimpleDateFormat(
inputFormat).parse(inputTimeStamp));
}
示例用法如下:
try {
String inputTimeStamp = "08/16/2011";
final String inputFormat = "MM/dd/yyyy";
final String outputFormat = "EEE MMM dd HH:mm:ss z yyyy";
System.out.println(TimeStampConverter(inputFormat, inputTimeStamp,
outputFormat));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
发布于 2012-03-26 21:06:44
String newstr = "08/16/2011";
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format = new SimpleDateFormat("EE MMM dd hh:mm:ss z yyyy");
Calendar c = Calendar.getInstance();
c.setTime(format1.parse(newstr));
System.out.println(format.format(c.getTime()));
https://stackoverflow.com/questions/9872419
复制相似问题