我想将此字符串转换为特定日期的日历对象,但它所做的只是给我当前日期
I wanted to convert this string into calendar object of the specific date, but all it does is giving me the current date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String startDateStr = stripQuotes("2017/08/01 15:18:01");
Date startDateDate;
Calendar startDate = null;
try {
startDateDate = dateFormat.parse(startDateStr)
startDate = Calendar.getInstance();
startDate.setTime(startDateDate);
} catch (ParseException ex) {
Logger.getLogger(AttendanceController.class.getName()).log(Level.SEVERE, null, ex);
}
Honey honey1 = new Honey(1,"ok","ok2",startDate);
我只是 运行 这个代码并且对我来说工作正常。 startDate.getTime()
returns Tue Aug 01 15:18:01 PDT 2017
,符合预期。
唯一的问题是你在 startDateDate = dateFormat.parse(startDateStr)
.
行的末尾少了一个分号
这也可能对您有所帮助:
tl;博士
LocalDateTime.parse(
"2017/08/01 15:18:01" ,
DateTimeFormatter.ofPattern( "uuuu/MM/dd HH:mm:ss" , Locale.US )
).atOffset( ZoneOffset.UTC )
旧版 类
正如其他人所说,您的代码应该按预期工作。
// Old outmoded way using troublesome legacy classes.
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse( input ) ;
Calendar cal = Calendar.getInstance() ;
cal.setTime( date ) ;
System.out.println( "date.toString(): " + date ) ;
你可以看到 code run live at IdeOne.com.
date.toString(): Tue Aug 01 15:18:01 GMT 2017
你有一个更大的问题。您正在使用非常麻烦的旧日期时间 类,现在已成为遗留问题,已被 java.time 类 取代。像瘟疫一样避免这些旧的类。
ISO 8601
顺便说一下,您使用的格式不太理想,无法将日期时间表示为字符串。而是使用标准 ISO 8601 格式。当 parsing/generating 字符串时,java.time 类 默认使用 ISO 8601 格式。您可以在下一个示例中看到。
并指定一个时区。省略偏移量或区域会导致混乱、错误和痛苦。
java.time
让我们以现代方式重写该代码。
// New modern way in Java 8 and later.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/MM/dd HH:mm:ss" , Locale.US ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
问题中的示例代码表明输入字符串表示的日期时间旨在表示 UTC 中的时刻(UTC 偏移量为零)。我们上面的 LocalDateTime
对象没有区域或偏移量,因此 不 代表时间轴上的一个点。在您分配 offset/zone 之前,此对象没有明确的含义。
// If we assume this date-time was meant to be UTC.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
System.out.println( "odt.toString(): " + odt ) ;
你可以看到 code run live at IdeOne.com.
odt.toString(): 2017-08-01T15:18:01Z
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String startDateStr = stripQuotes("2017/08/01 15:18:01");
Date startDateDate;
Calendar startDate = null;
try {
startDateDate = dateFormat.parse(startDateStr)
startDate = Calendar.getInstance();
startDate.setTime(startDateDate);
} catch (ParseException ex) {
Logger.getLogger(AttendanceController.class.getName()).log(Level.SEVERE, null, ex);
}
Honey honey1 = new Honey(1,"ok","ok2",startDate);
我只是 运行 这个代码并且对我来说工作正常。 startDate.getTime()
returns Tue Aug 01 15:18:01 PDT 2017
,符合预期。
唯一的问题是你在 startDateDate = dateFormat.parse(startDateStr)
.
这也可能对您有所帮助:
tl;博士
LocalDateTime.parse(
"2017/08/01 15:18:01" ,
DateTimeFormatter.ofPattern( "uuuu/MM/dd HH:mm:ss" , Locale.US )
).atOffset( ZoneOffset.UTC )
旧版 类
正如其他人所说,您的代码应该按预期工作。
// Old outmoded way using troublesome legacy classes.
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse( input ) ;
Calendar cal = Calendar.getInstance() ;
cal.setTime( date ) ;
System.out.println( "date.toString(): " + date ) ;
你可以看到 code run live at IdeOne.com.
date.toString(): Tue Aug 01 15:18:01 GMT 2017
你有一个更大的问题。您正在使用非常麻烦的旧日期时间 类,现在已成为遗留问题,已被 java.time 类 取代。像瘟疫一样避免这些旧的类。
ISO 8601
顺便说一下,您使用的格式不太理想,无法将日期时间表示为字符串。而是使用标准 ISO 8601 格式。当 parsing/generating 字符串时,java.time 类 默认使用 ISO 8601 格式。您可以在下一个示例中看到。
并指定一个时区。省略偏移量或区域会导致混乱、错误和痛苦。
java.time
让我们以现代方式重写该代码。
// New modern way in Java 8 and later.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/MM/dd HH:mm:ss" , Locale.US ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
问题中的示例代码表明输入字符串表示的日期时间旨在表示 UTC 中的时刻(UTC 偏移量为零)。我们上面的 LocalDateTime
对象没有区域或偏移量,因此 不 代表时间轴上的一个点。在您分配 offset/zone 之前,此对象没有明确的含义。
// If we assume this date-time was meant to be UTC.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
System.out.println( "odt.toString(): " + odt ) ;
你可以看到 code run live at IdeOne.com.
odt.toString(): 2017-08-01T15:18:01Z