的Java解析日期时间毫秒为日期越来越解析错误
我在我与毫秒发现日期时间如下字符串:的Java解析日期时间毫秒为日期越来越解析错误
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1; // Note: zero based!
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
int millis = now.get(Calendar.MILLISECOND);
String monthup = String.valueOf(month);
String dayup = String.valueOf(day);
String hourup = String.valueOf(hour);
String minuteup = String.valueOf(minute);
String secondup = String.valueOf(second);
String millisup = String.valueOf(millis);
if(monthup.length()==1){monthup="0"+monthup;}
if(dayup.length()==1){dayup="0"+dayup;}
if(hourup.length()==1){hourup="0"+hourup;}
if(minuteup.length()==1){minuteup="0"+minuteup;}
if(secondup.length()==1){secondup="0"+secondup;}
if(millisup.length()==1){millisup="0"+millisup;}
if(millisup.length()==2){secondup="00"+millisup;}
String timewithmilsec = year+ monthup + dayup+ hourup+ minuteup+ secondup+ millisup;
System.out.println(timewithmilsec);
我得到一个值:20151020115216690
这是obviousely正确的。我想解析它java Date format
。 我所做的是如下:
try{
DateFormat formatter = new SimpleDateFormat("yyyyMMdHHmmssaaa");
Date date = formatter.parse(timewithmilsec);
System.out.println(date);
}
catch(Exception e){
System.out.println(e);
}
我正在一个错误,如下所示:
java.text.ParseException: Unparseable date: "20151020115247995"
你只有一个d
在您的格式,但一天被填充到两个字符,另外,根据JavaDocs ...
a Am/pm marker Text PM
这不是毫秒占位符,我想你意味着SSS
例如...
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1; // Note: zero based!
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
int millis = now.get(Calendar.MILLISECOND);
String monthup = String.valueOf(month);
String dayup = String.valueOf(day);
String hourup = String.valueOf(hour);
String minuteup = String.valueOf(minute);
String secondup = String.valueOf(second);
String millisup = String.valueOf(millis);
if (monthup.length() == 1) {
monthup = "0" + monthup;
}
if (dayup.length() == 1) {
dayup = "0" + dayup;
}
if (hourup.length() == 1) {
hourup = "0" + hourup;
}
if (minuteup.length() == 1) {
minuteup = "0" + minuteup;
}
if (secondup.length() == 1) {
secondup = "0" + secondup;
}
if (millisup.length() == 1) {
millisup = "0" + millisup;
}
if (millisup.length() == 2) {
secondup = "00" + millisup;
}
String timewithmilsec = year + monthup + dayup + hourup + minuteup + secondup + millisup;
System.out.println(timewithmilsec);
try {
DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date date = formatter.parse(timewithmilsec);
System.out.println(date);
} catch (Exception e) {
System.out.println(e);
}
这对我打印
20151020173034124
Tue Oct 20 17:30:34 EST 2015
虽然我吧,让我给你介绍String.format
,可你int
减少String
转换和填充代码到...
String timewithmilsec = String.format("%04d%02d%02d%02d%02d%02d%03d", year, month, day, hour, minute, second, millis);
我得到周二10月20日12时04分08秒IST 2015年,但有趣的是,我没有看到这里的任何毫秒
Date#toString
将不包括默认毫秒,你的Wi我需要提供一个DateFormat
这可以。
如果我System.out.println(new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS").format(date));
替换最后System.out.println
它打印像
20 Oct 2015 17:37:14.856
(该值20151020173714856
)
我得到'Tue Oct 20 12:04:08 IST 2015',但有趣的是我没有在这里看到任何毫秒 – Santhucool
@Santhucool'Date#toString'通常不会打印出“毫秒”,您需要提供一种格式 – MadProgrammer
确定好友,如果你能帮我把'Tue Oct 20 12:04:08 IST 2015'变成'Tue Oct 20 12:04:08 IST 2015''那么我的问题就解决了!我的意思是转换后的另一种方式! – Santhucool
根据文档信S
响应毫秒所以您的格式应该是这样new SimpleDateFormat("yyyyMMddHHmmssSSS");
(你在您的格式中有一个d
)。
我得到'周二10月20日12时04分08秒IST 2015'但有趣的是我没有在这里看到的毫秒数 – Santhucool
如果你想以毫秒为单位打印日期,你应该创建另一个'SimpleDateFormat'的实例,其格式为你想要的输出,然后像'System.out.println(sdf.format(date))一样打印出来;''其中'sdf'是'SimpleDateFormat'的新实例,'date'是代码中的日期。 – wawek
答案是缺少“d”的日期格式,其中@MadProgrammer描述。
另外,应该重新考虑日期的字符串表示的生成。您应该使用SimpleDateFormat.format()
生成日期字符串,如下面的代码示例:
Calendar now = Calendar.getInstance();
DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String formattedDate = formatter.format(now.getTime());
System.out.println("Formatted date: " + formattedDate);
和输出将在您请求的格式。
Formatted date: 20151020094934279
你只有一个'D'和'aaa'没有做什么你(显然)认为它做 – MadProgrammer