java-time

Serialize LocalDate and LocalDateTime as Unix timestamps

那年仲夏 提交于 2019-12-05 13:52:49
I'm moving from using java.sql.Timestamp and java.util.GregorianCalendar to employ java.time.* new classes in a Spring MVC application. So I changed every private GregorianCalendar field; to private LocalDate field; or private LocalDateTime field; But now when serializing those beans, they get serialized like this: "field": { "year": 1970, "month": "JANUARY", "dayOfMonth": 18, "dayOfWeek": "SUNDAY", "era": "CE", "dayOfYear": 18, "leapYear": false, "monthValue": 1, "chronology": { "id": "ISO", "calendarType": "iso8601" } }, I found answers to other questions that mention to add a dependency to

Java 8: How to create DateTimeFormatter with milli, micro or nano seconds?

眉间皱痕 提交于 2019-12-05 12:39:01
I need to create the formatter for parsing timestamps with optional milli, micro or nano fractions of second. For example, for my needs I see the following opportunity: DateTimeFormatter formatter = new DateTimeFormatterBuilder() .append(DateTimeFormatter.BASIC_ISO_DATE) .appendLiteral('-') .append(DateTimeFormatter.ISO_LOCAL_TIME) .appendOffset("+HH:mm", "Z") .toFormatter(); Or it is also possible to use appendFraction(field, minWidth, maxWidth, decimalPoint) . However in these cases will it be possible to parse timestamps with any number of decimals (up to 9 or maxWidth). How to achieve that

Getting time range between midnight and current time JDK 8

廉价感情. 提交于 2019-12-05 12:36:20
I have this method to calculate midnigt and current time as long values: /** * Returns the time range between the midnight and current time in milliseconds. * * @param zoneId time zone ID. * @return a {@code long} array, where at index: 0 - midnight time; 1 - current time. */ public static long[] todayDateRange(ZoneId zoneId) { long[] toReturn = new long[2]; LocalTime midnight = LocalTime.MIDNIGHT; LocalDate today = LocalDate.now(zoneId); LocalDateTime todayMidnight = LocalDateTime.of(today, midnight); ZonedDateTime todayMidnightZdt = todayMidnight.atZone(zoneId); toReturn[0] =

Can I create only one static instance of DateTimeFormatter

谁说胖子不能爱 提交于 2019-12-05 09:52:12
Is it possible to create only one static instance of DateTimeFormatter and use it everywhere in my project, instead of creating it multiple times? public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); Can there be thread-safety issues in such cases? Yes, it is possible. Your variable is Thread Safe because it is immutable - No other thread can change it. Other solutions are also possible in your specific case, but I am always in favour of immutable objects. See Java Concurrency in practice Chapter 3. By the way, the chapter seems to be free to browse now,

Parsing date without month using DateTimeFormatter

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 07:30:38
I try to parse a date with this format: ddYYYY . For example, I have the string 141968 , and I want to know that day = 14 and year = 1968 . I suppose I have to use directly a TemporalAccessor gave by DateTimeFormatter.parse(String) , but I cannot find how to use this result. While debugging I see the result is a java.time.Parsed which is not public but contains informations I want in field fieldValues . How can I parse this particular format? Thank you. One approach is to default the missing month field: DateTimeFormatter f = new DateTimeFormatterBuilder() .appendPattern("ddyyyy")

(Simple)DateFormat that allow 24:00:00 and 00:00:00 as inputs

久未见 提交于 2019-12-05 07:28:05
I've been looking for this for a while, with no success so far. Do you know if there's a "DateFormat" ish class, that will allow me to use "00:00:00" and "24:00:00" as input parameters (they're both midnight) but when called "getHour()" I'll get 0 or 24 as a result? Using "kk" will only allow me to have <1:24> range, meanwhile I'm looking for <0:24> range formatting The value 24:00 is not represented in a LocalTime because it is strictly part of the next day. Consideration was given to models where 24:00 could be represented as part of LocalTime , but the conclusion was that it would be very

Calculate Modified Julian Day in JSR-310

不问归期 提交于 2019-12-05 06:38:09
How can I calculate the Modified Julian day from a JSR-310 class such as LocalDate ? (in JDK 8) Specifically, this is the calculation of the continuous count of days known as "Modified Julian day", not the date in the Julian calendar system . For example: LocalDate date = LocalDate.now(); long modifiedJulianDay = ??? Short answer: LocalDate date = LocalDate.now(); long modifiedJulianDay = date.getLong(JulianFields.MODIFIED_JULIAN_DAY); Explanation: The Wikipedia article gives the best description of Julian day as a concept. Put simply, it is a simple, continuous, count of days from some epoch,

Parse ISO date by Period in Java 8

雨燕双飞 提交于 2019-12-05 03:13:19
i want to replace JodaTime by Java 8 DateTime API . I've got ISO-8601 period described = P2W5DT11H8M In JodaTime i parse it very simply by executing the following code: Period.parse("P2W5DT11H8M") and i get the successful Period object. Can i do the same in Java 8? A Period in Java 8 only has year/month/day components. A Duration has hour/minute/second components. It seems that you will need to parse the string manually. One option could look like the code below (you need to add input validation etc.) - there may be better alternatives. public static void main(String[] args) { System.out

How to get the current hour with new Date-Time API in Java 8?

邮差的信 提交于 2019-12-05 01:08:50
Before Java 8 the common method was the Calendar API: Calendar cal = Calendar.getInstance(); int hour = cal.get(Calendar.HOUR_OF_DAY); How can I get the current hour, but using the new Date-Time API provided in Java 8 ? This all depends, but something like... LocalTime now = LocalTime.now(); System.out.println(now.getHour()); Should work just fine... Take a closer look at LocalTime#getHour for more details 来源: https://stackoverflow.com/questions/25539891/how-to-get-the-current-hour-with-new-date-time-api-in-java-8

Wildcard in DateTimeFormatter

六眼飞鱼酱① 提交于 2019-12-05 01:07:50
I need to parse a string into a LocalDate . The string looks like 31.* 03 2016 in regex terms (i.e. .* means that there may be 0 or more unknown characters after the day number). Example input/output: 31xy 03 2016 ==> 2016-03-31 I was hoping to find a wildcard syntax in the DateTimeFormatter documentation to allow a pattern such as: LocalDate.parse("31xy 03 2016", DateTimeFormatter.ofPattern("dd[.*] MM yyyy")); but I could not find anything. Is there a simple way to express optional unknown characters with a DateTimeFormatter ? ps: I can obviously modify the string before parsing it but that's