我想知道为什么'Y'会回归2012而'y'会在2011年回归SimpleDateFormat:
System.out.println(new SimpleDateFormat("Y").format(new Date())); // prints 2012
System.out.println(new SimpleDateFormat("y").format(new Date())); // prints 2011
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释原因吗?
以下代码:
Calendar now = Calendar.getInstance();
month = now.get(Calendar.MONTH) + 1;
year = now.get(Calendar.YEAR);
System.out.println("Month " + month + " year " + year);
SimpleDateFormat dt1 = new SimpleDateFormat("MMMM YYYY");
e.setMonthnYear(dt1.format(now.getTime()));
Run Code Online (Sandbox Code Playgroud)
在服务器上部署后显示以下异常:
java.lang.IllegalArgumentException: Illegal pattern character 'Y'
java.text.SimpleDateFormat.compile(SimpleDateFormat.java:768)
java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:575)
java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:500)
java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:475)
iland.employee.EmployeeAction.fetchAllAtted(EmployeeAction.java:169)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
Run Code Online (Sandbox Code Playgroud)
在我使用的本地主机上JDK v1.8,上面的代码工作正常,但在服务器上它无法正常工作.
我该如何解决这个问题?
我在SO中有关于序列化和反序列化java.time.LocalDateTime以及JSON属性的解答已经阅读了几个问题,但我似乎无法使其正常工作.
我已经设法配置我的Spring Boot应用程序以我希望的格式返回日期(YYY-MM-dd HH:mm)但是我在接受JSON中的这种格式的值时遇到了问题.
这些是我到目前为止所做的所有事情:
添加了maven依赖关系jsr310:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
jsr310在我的主要课程中指定:
@EntityScan(basePackageClasses = { App.class, Jsr310JpaConverters.class })
Run Code Online (Sandbox Code Playgroud)
禁用序列化作为时间戳application.properties:
spring.jackson.serialization.write_dates_as_timestamps=false
Run Code Online (Sandbox Code Playgroud)
这是我的日期时间实体映射:
@Column(name = "start_date")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
@JsonFormat(pattern = "YYYY-MM-dd HH:mm")
private LocalDateTime startDate;
Run Code Online (Sandbox Code Playgroud)
在我的数据库中,我将此日期存储为TIMESTAMP,格式如下:2016-12-01T23:00:00+00:00.
如果我通过我的控制器访问此实体,它将返回具有正确startDate格式的JSON.当我尝试发布它并反序列化时,使用YYYY-MM-dd HH:mm格式,我得到以下异常:
{
"timestamp": "2016-10-30T14:22:25.285+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read document: Can not deserialize value of type java.time.LocalDateTime from String \"2017-01-01 20:00\": Text '2017-01-01 20:00' could not …Run Code Online (Sandbox Code Playgroud) 我这里有一个非常简单的程序:
public static void main(String[] args) {
LocalDate year = LocalDate.ofYearDay(2022, 100);
System.out.println(year);
System.out.println(WeekFields.of(Locale.GERMAN).weekOfYear());
System.out.println(year.with(WeekFields.of(Locale.GERMAN).weekOfYear(), 0));
System.out.println(year.with(WeekFields.of(Locale.GERMAN).weekOfYear(), 0).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)));
}
Run Code Online (Sandbox Code Playgroud)
但是它在JVM 8和JVM 10上的行为有所不同。问题似乎出在的实现上WeekFields.of(Locale.GERMAN).weekOfYear()。
在JVM 10上,我得到以下结果:
JVM 10
2022-04-10
WeekOfYear[WeekFields[SUNDAY,1]]
2021-12-19
2021-12-13
Run Code Online (Sandbox Code Playgroud)
而在JVM 8上:
JVM 8
2022-04-10
WeekOfYear[WeekFields[MONDAY,4]]
2022-01-02
2021-12-27
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?我是否正在做某事,可能会导致不确定的行为?还是在某处指定了这种行为变化?
JVM10:
$ java -version
openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)
Run Code Online (Sandbox Code Playgroud)
JVM8
$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, …Run Code Online (Sandbox Code Playgroud) 我有两个相关的问题.
假设在BST中运行的程序以UTC格式生成当前时间的日期时间值YYYY-MM-DDTHH:MM:SS.SSSZ格式
同时假设伦敦当前时间是2016-06-01 12:33:54
1)如果程序给出的当前时间是2016-06-01T11:33:54.000Z,程序错了吗?
2)如何在YYYY-MM-DDTHH的相应时间格式中记录BST的夏季偏移:MM:SS.SSSZ
我假设YYYY-MM-DDTHH:MM:SS + 0001我是否正确?
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, YYYY, EEE", Locale.US);
System.out.println(sdf.format(new Date()));
System.out.println(sdf.format(sdf.parse("Apr 27, 2018, Fri")));
Run Code Online (Sandbox Code Playgroud)
Java不会按预期解析日期并输出:
Apr 27, 2018, Fri
Jan 05, 2018, Fri // I can not understand why java parse the month of April as January
Run Code Online (Sandbox Code Playgroud)