以UTC格式将LocalDateTime转换为LocalDateTime.
LocalDateTime convertToUtc(LocalDateTime date) {
//do conversion
}
Run Code Online (Sandbox Code Playgroud)
我在网上搜索过.但没有得到解决方案
在DateTimeFormatter类文档定义了不同的符号u为一年,y一年的时代:https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns
年份和年份有什么区别?
我评估我的项目从使用迁移乔达时间到java.time包中的Java 8.在Joda-Time,我大量使用了这门Interval课程.我在java.time中找不到这样的东西.
有类似的课吗?
我正在为我的新项目使用Java 8.
我正在尝试在java 8中使用新的日期和时间api但是我不知道是否JPA 2.1完全支持这个新的日期和时间API.
请在JPA支持Java 8中的新日期和时间API中分享您的经验/意见.
我可以使用JPA 2.1安全地在Java 8中使用新的日期和时间api吗?
更新:
我正在使用Hibernate(4.3.5.Final)作为JPA实现.
在Java 8及更高版本中可用Duration的新JSR 310 date API(java.time包)中的类中,javadoc说:
此类以秒和纳秒为单位模拟时间量或时间量.它可以使用其他基于持续时间的单位进行访问,例如分钟和小时.此外,DAYS单位可以使用,并且被视为完全等于24小时,因此忽略了夏令时效果.
那么,为什么以下代码崩溃?
Duration duration = Duration.ofSeconds(3000);
System.out.println(duration.get(ChronoUnit.MINUTES));
Run Code Online (Sandbox Code Playgroud)
这提出了一个UnsupportedTemporalTypeException:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Minutes
at java.time.Duration.get(Duration.java:537)
Run Code Online (Sandbox Code Playgroud)
那么从持续时间对象中提取分钟和小时的推荐方法是什么?我们是否必须从秒数开始计算?为什么这样实现?
这些之间有什么区别:
zonedDateTime.truncatedTo(ChronoUnit.DAYS);
zonedDateTime.toLocalDate().atStartOfDay(zonedDateTime.getZone());
Run Code Online (Sandbox Code Playgroud)
有理由偏爱另一个吗?
谢谢
是否可以在新java.timeAPI中获取两个日期之间的所有日期?
假设我有这部分代码:
@Test
public void testGenerateChartCalendarData() {
LocalDate startDate = LocalDate.now();
LocalDate endDate = startDate.plusMonths(1);
endDate = endDate.withDayOfMonth(endDate.lengthOfMonth());
}
Run Code Online (Sandbox Code Playgroud)
现在我需要startDate和之间的所有日期endDate.
我想要得到daysBetween两个日期并迭代:
long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
for(int i = 0; i <= daysBetween; i++){
startDate.plusDays(i); //...do the stuff with the new date...
}
Run Code Online (Sandbox Code Playgroud)
有更好的方法来获取日期吗?
什么是之间进行转换的正确方法java.sql.Date和LocalDate(Java8)?
在Wikipedia上,时区偏移量解释为与标准UTC时间的小时和分钟差异。但是,DateTimeFormatter支持zone-offset模式XXXXX,该模式“输出时,分和可选秒,并带有冒号,例如'+01:30:15'”。
+01:30:15ISO等偏移量有效吗?如果不是,那么Java根据哪个标准定义此类偏移量?
我对Java 8 Time API中的日期解析感到有点气馁.
以前我可以轻松写:
String date = "04.2013";
DateFormat df = new SimpleDateFormat("MM.yyyy");
Date d = df.parse(date);
Run Code Online (Sandbox Code Playgroud)
但是现在如果我使用LocalDate并像这样做:
String date = "04.2013";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.yyyy");
LocalDate ld = LocalDate.parse(date, formatter);
Run Code Online (Sandbox Code Playgroud)
我收到一个例外:
java.time.format.DateTimeParseException: Text '04' could not be parsed at index 0
java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
java.time.LocalDate.parse(LocalDate.java:400)
java.time.LocalDate.parse(LocalDate.java:385)
com.luxoft.ath.controllers.JsonController.region(JsonController.java:38)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Run Code Online (Sandbox Code Playgroud)
如果我将字符串格式更改为"yyyy-MM-dd",即使没有格式化程序,一切也能正常工作:
String date = "2013-04-12";
LocalDate …Run Code Online (Sandbox Code Playgroud)