java-time

How to format YearMonth and MonthDay depending on a Locale?

半城伤御伤魂 提交于 2019-11-27 15:15:23
Formatting a LocalDate in Java 8 using a specific Locale can be achieved like this: DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(myLocale).format(value); DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(myLocale).format(value); DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(myLocale).format(value); DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(myLocale).format(value); Assuming value = LocalDate.now() this would result into: // myLocale = Locale.ENGLISH 6/30/16 Jun 30, 2016 June 30, 2016 Thursday, June 30, 2016 // myLocale =

Persist java.time.Instant (JDK8) with JPA2/Hibernate

点点圈 提交于 2019-11-27 15:03:17
Neither JPA nor Hibernate currently support the new date/time classes brought by JSR-310 in JDK8 (JPA ticket , Hibernate ticket ). Nonetheless, I'd like to code with the JDK8 date/time classes as they are finally well designed. In particular, I'm interested in java.time.Instant , not in full support for all java.time.* types, as all my entities will use this particular class (or so I think now, at least :-) One option is to write a type converter , as defined by JPA 2.1. However, our app server is JBoss EAP 6.3 which is JPA 2.0 but not 2.1 compatible, so this is out of the question for now.

Android N Java8 java.time

扶醉桌前 提交于 2019-11-27 15:01:19
I updated to the latest Android N sdk. The only thing I don't understand is why I cannot import java.time into my code? I thought Java8 is available through Android N. Then why didn't Google add java.time package? Maheshwar Ligade Android N is not supporting all the features of Java 8. Following features are only supported: Default and static interface methods Lambda expressions Repeatable annotations Reflection and language-related APIs: java.lang.FunctionalInterface java.lang.annotation.Repeatable java.lang.reflect.Method.isDefault() and Reflection APIs associated with repeatable annotations

How to get previous month and years in java?

喜欢而已 提交于 2019-11-27 14:35:35
How do I find out the last month and its year in java e.g. If today is 10th oct 2012, result should be Month = 9 and year = 2012, If today is 10th jan 2013, result should be Month = 12 and year = 2012 Gaim Your solution is here but instead of addition you need to use subtraction c.add(Calendar.MONTH, -1); Then you can call getter on the Calendar to acquire proper fields int month = c.get(Calendar.MONTH) + 1; // beware of month indexing from zero int year = c.get(Calendar.YEAR); Przemek java.time Using java.time framework built into Java 8: import java.time.LocalDate; LocalDate now = LocalDate

How to obtain the end of the day when given a LocalDate?

故事扮演 提交于 2019-11-27 14:18:29
问题 How to obtain the end of the day when given a LocalDate? I could get it by doing LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59)); But is there an equivalent 'atStartOfDay' method for the end of the day? LocalDate.now().atStartOfDay(); LocalDate.now().atEndOfDay(); //doesn't work 回答1: Here are a few alternatives, depending on what you need: LocalDate.now().atTime(23, 59, 59); //23:59:59 LocalDate.now().atTime(LocalTime.MAX); //23:59:59.999999999 But there is no built-in method. As

Can't parse String to LocalDate (Java 8)

限于喜欢 提交于 2019-11-27 13:56:14
My input is a String representation of a date in the format "01-07-2015" for July 1, 2015. I'm trying to parse this into a java.time.LocalDate variable: final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd-MM-YYYY"); final String input = "01-07-2015"; final LocalDate localDate = LocalDate.parse(input, DATE_FORMAT); Based on the DateTimeFormatter JavaDoc , I would expect this to work. However, I'm greeted with a very friendly and helpful message: Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=1, MonthOfYear=7, WeekBasedYear

Is Joda Time deprecated with java 8 Date and Time API? (java.time)

老子叫甜甜 提交于 2019-11-27 13:52:27
Is there any reason to use Joda Time if I can use Java 8 Date and Time API ( java.time )? Should I use Java 8 Date and Time every time? The official statement of the author of Joda-time himself is to migrate as soon as Java-8 is available . See also this citation from the website : Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310). So the short answer to your question is: YES (deprecated). However, keep in mind that some features like Joda- Interval or PeriodType or PeriodFormatter

Round minutes to ceiling using Java 8

匆匆过客 提交于 2019-11-27 13:41:37
问题 So I'm lucky enough to use Java 8 and the new time APi but I don't see any rounding functions... Basically if the time is... 2014-08-28T10:01.00.000 ----> 2014-08-28T10:02.00.000 2014-08-28T10:01.10.123 ----> 2014-08-28T10:02.00.000 2014-08-28T10:01.25.123 ----> 2014-08-28T10:02.00.000 2014-08-28T10:01.49.123 ----> 2014-08-28T10:02.00.000 2014-08-28T10:01.59.999 ----> 2014-08-28T10:02.00.000 This seems to be ok, but is it right? LocalDateTime now = LocalDateTime.now(Clock.systemUTC());

Java 8 LocalDate - How do I get all dates between two dates?

こ雲淡風輕ζ 提交于 2019-11-27 13:00:58
Is there a usablility to get all dates between two dates in the new java.time API? Let's say I have this part of code: @Test public void testGenerateChartCalendarData() { LocalDate startDate = LocalDate.now(); LocalDate endDate = startDate.plusMonths(1); endDate = endDate.withDayOfMonth(endDate.lengthOfMonth()); } Now I need all dates between startDate and endDate . I was thinking to get the daysBetween of the two dates and iterate over: long daysBetween = ChronoUnit.DAYS.between(startDate, endDate); for(int i = 0; i <= daysBetween; i++){ startDate.plusDays(i); //...do the stuff with the new

long timestamp to LocalDateTime

白昼怎懂夜的黑 提交于 2019-11-27 12:55:50
问题 I have a long timestamp 1499070300 (equivalent to Mon, 03 Jul 2017 16:25:00 +0800) but when I convert it to LocalDateTime I get 1970-01-18T16:24:30.300 Here's my code long test_timestamp = 1499070300; LocalDateTime triggerTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone .getDefault().toZoneId()); 回答1: You need to pass timestamp in milliseconds: long test_timestamp = 1499070300000L; LocalDateTime triggerTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(test