java-time

JDK dateformatter parsing DayOfWeek in German locale, java8 vs java9

旧街凉风 提交于 2019-11-27 09:03:42
I have tried some code in Java 8 (1.8.0_77) and Java 9 (Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)) DateTimeFormatter dtf = DateTimeFormatter.ofPattern("eee", Locale.GERMAN); DayOfWeek mo = dtf.parse("Mo", DayOfWeek::from); System.out.println("mo = " + mo); I am not too familiar with details of those classes, but in Java 8 this works, printing: mo = MONDAY In Java 9, however it fails Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mo' could not be parsed at index 0 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java

Getting ZoneId from a SimpleTimeZone

一个人想着一个人 提交于 2019-11-27 08:49:49
问题 Using Java I have a SimpleTimeZone instance with GMT offset and daylight saving time information from a legacy system. I would like to retrieve ZoneId to be able to use Java 8 time API. Actually, toZoneId returns a ZoneId without the daylight Saving time infos SimpleTimeZone stz = new SimpleTimeZone( 2 * 60 * 60 * 1000, "GMT", Calendar.JANUARY,1,1,1, Calendar.FEBRUARY,1,1,1, 1 * 60 * 60 * 1000); stz.toZoneId(); 回答1: First of all, when you do: SimpleTimeZone stz = new SimpleTimeZone(2 * 60 *

Writing and testing convenience methods using Java 8 Date/Time classes

限于喜欢 提交于 2019-11-27 08:43:35
问题 I have some old convenience methods, written using Calendars, that I want to update to use the Java.time.* classes introduced in Java 8. Some of the methods in my class get quantities like the current time or even just the current hour. I plan to write two variants of each method: one that assumes that the timezone is the default defined on this computer and one that allows the user to specify the desired timezone. I'm trying to figure out two main things: How to get the current timestamp in

Exception when trying to parse a LocalDateTime

放肆的年华 提交于 2019-11-27 08:27:52
问题 I am using the following timestamp format: yyyyMMddHHmmssSSS The following method works fine: public static String formatTimestamp(final Timestamp timestamp, final String format) { final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return timestamp.toLocalDateTime().format(formatter); } And, when I pass in a Timestamp with that format string, it returns, for example: 20170925142051591 I then require to map from that string to a Timestamp again, essentially the reverse

JSR-310 - parsing seconds fraction with variable length

流过昼夜 提交于 2019-11-27 08:15:44
Is there a way how to create JSR-310 formatter that is able to parse both following date/times with variable length of seconds fraction? 2015-05-07 13:20:22.276052 or 2015-05-07 13:20:22.276 Example code: DateTimeFormatter formatter = new java.time.format.DateTimeFormatterBuilder() .append( java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") ) .appendOptional( java.time.format.DateTimeFormatter.ofPattern(".SSSSSS") ) .toFormatter(); formatter.parse("2015-05-07 13:20:22.276052", LocalDateTime::from); JodaStephen This solves the problem: DateTimeFormatter formatter = new

Why does the new Java 8 Date Time API not have nanosecond precision? [duplicate]

偶尔善良 提交于 2019-11-27 07:43:45
This question already has an answer here: Java 8 Instant.now() with nanosecond resolution? 4 answers One of the features of the new Date Time API in Java 8 is supposed to be nanosecond precision. However when I print the current Date Time to the console like so DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd'T'HH:mm:ss,nnnnnnnnnZ"); System.out.println(OffsetDateTime.now().format(formatter)); I only see millisecond precision: 2015-11-02T12:33:26,746000000+0100 The operating system does seem to support nanosecond precision. When I print the current date time via the

How to extract epoch from LocalDate and LocalDateTime?

梦想与她 提交于 2019-11-27 07:31:28
How do I extract the epoch value to Long from instances of LocalDateTime or LocalDate ? I've tried the following, but it gives me other results: LocalDateTime time = LocalDateTime.parse("04.02.2014 19:51:01", DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")); System.out.println(time.getLong(ChronoField.SECOND_OF_DAY)); // gives 71461 System.out.println(time.getLong(ChronoField.EPOCH_DAY)); // gives 16105 What I want is simply the value 1391539861 for the local datetime "04.02.2014 19:51:01" . My timezone is Europe/Oslo UTC+1 with daylight saving time. The classes LocalDate and LocalDateTime

Java8 DateTimeFormatter am/pm

妖精的绣舞 提交于 2019-11-27 05:54:28
问题 I am trying to parse some dates, but the DateTimeParser seems to disagree with me on what is valid import java.time.ZonedDateTime import java.time.format.DateTimeFormatter import java.util.Locale ZonedDateTime.parse("Wed Jul 16, 2016 4:38pm EDT", DateTimeFormatter.ofPattern("EEE MMM dd, yyyy hh:mma z", Locale.US)) When I try this it says java.time.format.DateTimeParseException: Text 'Wed Jul 16, 2016 4:38pm EDT' could not be parsed at index 17 So something is wrong with the hours? When I drop

Hibernate with Java 8 LocalDate & LocalDateTime in Database

て烟熏妆下的殇ゞ 提交于 2019-11-27 05:29:10
问题 My requirement is to store all dates & date-times in UTC timezone in the database. I am using Java 8's LocalDate & LocalDateTime in my Hibernate entities. Is that correct as LocalDate & LocalDateTime doesn't have timezone associated with them? If not, should I fall back to using good old (or legacy?) Date & Timestamp ? Or should I be using Java 8's Instant ? If using Instant , will there be a possibility to store only the date part, without time? The database is MySQL & SQL Server and this is

DateTimeFormatter month pattern letter “L” fails

谁说胖子不能爱 提交于 2019-11-27 04:59:17
I noticed that java.time.format.DateTimeFormatter is not able to parse out as expected. See below: import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Play { public static void tryParse(String d,String f) { try { LocalDate.parse(d, DateTimeFormatter.ofPattern(f)); System.out.println("Pass"); } catch (Exception x) {System.out.println("Fail");} } public static void main(String[] args) { tryParse("26-may-2015","dd-L-yyyy"); tryParse("26-May-2015","dd-L-yyyy"); tryParse("26-may-2015","dd-LLL-yyyy"); tryParse("26-May-2015","dd-LLL-yyyy"); tryParse("26-may-2015","dd-M