DateTimeFormatter当您尝试超出可能范围的日期时,Java 会抛出异常,例如:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/yyyy");
String dateString = "12/32/2015";
LocalDate ld = LocalDate.parse(dateString, dtf);
Run Code Online (Sandbox Code Playgroud)
将抛出:
Exception in thread "main" java.time.format.DateTimeParseException: Text '12/32/2015' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32
Run Code Online (Sandbox Code Playgroud)
但是当我输入一个无效的日历日期时,它的标准在语法上仍然是可能的,它会将其自动更正为有效日期,例如:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/yyyy");
String dateString = "2/31/2015";
LocalDate ld = LocalDate.parse(dateString, dtf);
Run Code Online (Sandbox Code Playgroud)
它成功解析但自动更正2015-02-28.我不想要这种行为,我希望它在日期不是有效的日历日期时仍然抛出异常.是否有可以为此设置的内置选项,或者我是否真的必须尝试手动筛选出这些实例?
如何转换具有以下格式的日期
2015年9月22日上午10:39:42
至
09/22/2015 10:39:42
在Java 8中?
我目前的代码:
String value = "September 22nd 2015, 10:39:42 am";
String format = "dd/MM/yyyy HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse(value);
System.out.println(date);
System.out.println(sdf.format(date));
}
catch (ParseException ex) {
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我试图解析以下时间戳字符串03-feb-2014 13:16:31使用java.time但它抛出一个错误.这是我的代码.
String timestamp = "03-feb-2014 13:16:31";
DateTimeFormatter format;
DateTimeFormatterBuilder formatBuilder = new DateTimeFormatterBuilder();
formatBuilder.parseCaseInsensitive();
formatBuilder.append(DateTimeFormatter.ofPattern("dd-MMM-YYYY HH:mm:ss"));
format = formatBuilder.toFormatter();
LocalDateTime localdatetime = LocalDateTime.parse(timestamp, format);
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误.
Exception in thread "main" java.time.format.DateTimeParseException: Text '03-feb-2014 13:16:31' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfMonth=3, MonthOfYear=2, WeekBasedYear[WeekFields[SUNDAY,1]]=2014},ISO resolved to 13:16:31 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at com.target.util.CntrlmProcessor.main(CntrlmProcessor.java:24)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfMonth=3, MonthOfYear=2, WeekBasedYear[WeekFields[SUNDAY,1]]=2014},ISO resolved to 13:16:31 …Run Code Online (Sandbox Code Playgroud) 是否可以构造一个java.time.DateFormatter等价的以下项?
Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());
assert instant.equals(Instant.now);
String str = String.valueOf(instant.toEpochMilli());
Instant instant = Instant.from(formatter.parse(String.valueOf(System.currentTimeMillis())));
assert instant.equals(Instant.now);
String str = formatter.format(instant);
Run Code Online (Sandbox Code Playgroud)
当您的文本输入使用历元时,不得不特殊情况显得有些可惜。
我有以下joda日期解析器:
ISODateTimeFormat.dateTimeParser().withOffsetParsed()。
我想将此重构为java.timeapi。但是,特别是在偏移量方面,与上面的解析器完全等效的是什么?
我的一个实体中有一个Java 8 LocalTime.
private final LocalTime departureTime;
Run Code Online (Sandbox Code Playgroud)
它是一个带有Spring Data Rest的Spring Boot 1.3.6应用程序.我使用Jsr310JpaConverters来支持JPA的Java 8时间模块.
当我将LocalTime变量保存到MySql时,保存LocalTime变量的日期也被持久化到数据库.假设我在2016年1月1日18:00:00保存,它将持续为2016-01-01 18:00:00.我只想节省时间.即18:00:00.有解决方案吗
提前致谢
我试图使用以下模式解析日期字符串:yyMMdd和STRICT解析器如下:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat).withResolverStyle(ResolverStyle.STRICT);
LocalDate.parse(expiryDate, formatter);
Run Code Online (Sandbox Code Playgroud)
我得到以下内容DateTimeParseException:
java.time.format.DateTimeParseException:无法解析文本'160501':无法从TemporalAccessor获取LocalDate:{YearOfEra = 2016,MonthOfYear = 5,DayOfMonth = 1},ISO类型为java.time.format.Parsed
当我切换到默认的解析风格时,即ResolverStyle.SMART允许这样的日期为2月30日.
有人可以帮忙吗?
我只找到了Joda Time的解决方案.
我的解决方案只有在最后一天不在第一周时才有效:
LocalDate.now() // or any other LocalDate
.withDayOfMonth(31)
.withMonth(12)
.get(weekFields.weekOfWeekBasedYear())
Run Code Online (Sandbox Code Playgroud)
那么Java Time中的正确方法是什么(比如Joda Time)?
我正在尝试将以GMT/UTC格式发送的XMLGregorianCalendar转换为America/Los_Angeles时区中的Java 8 LocalDateTime而没有运气.
这是我尝试过的,无法将时间转换为太平洋时间.
//xmlDate is 2017-11-13T00:00:00Z
ZonedDateTime zDateTime = xmlDate.toGregorianCalendar().toZonedDateTime().toLocalDateTime().atZone(ZoneId.of("America/Los_Angeles"));
LocalDateTime localDateTime = zDateTime.toLocalDateTime();
//Expected localDateTime is 2017-11-12T16:00. But I only get 2017-11-13T00:00
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
我尝试了一切,但错误仍未解决
这是我的代码:
public String[] getWeekDays() {
LocalDate today = LocalDate.now();
LocalDate monday = today;
LocalDate tue = today;
LocalDate wed = today;
LocalDate thur = today;
LocalDate fri = today;
LocalDate sat = today;
LocalDate sunday = today;
while (sunday.getDayOfWeek() != DayOfWeek.SUNDAY){
sunday = sunday.minusDays(1);
}
while (monday.getDayOfWeek() != DayOfWeek.MONDAY){
monday = monday.minusDays(1);
}
while (tue.getDayOfWeek() != DayOfWeek.TUESDAY){
tue = tue.plusDays(1);
}
while (wed.getDayOfWeek() != DayOfWeek.WEDNESDAY){
wed = wed.plusDays(1);
}
while (thur.getDayOfWeek() != DayOfWeek.THURSDAY){
thur = thur.plusDays(1);
}
while (fri.getDayOfWeek() …Run Code Online (Sandbox Code Playgroud)