java-time

Getting current time in Java 8

家住魔仙堡 提交于 2019-11-30 11:25:28
I am exploring the new java.time API of Java 8. I am particularly trying to retrieve the current time (my current time zone, of a different time zone, and of a different offset). The code is: public static void getCurrentLocalTime(){ LocalTime time = LocalTime.now(); System.out.println("Local Time Zone: "+ZoneId.systemDefault().toString()); System.out.println("Current local time : " + time); } public static void getCurrentTimeWithTimeZone(){ LocalDateTime localtDateAndTime = LocalDateTime.now(); ZoneId zoneId = ZoneId.of("America/Los_Angeles"); ZonedDateTime dateAndTimeInLA = ZonedDateTime.of

Java SE 8 TemporalAccessor.from issues when used with a java.time.Instant object

a 夏天 提交于 2019-11-30 10:50:20
java.time has an Instant class which encapsulates a position (or 'moment') on the timeline. While I understand that this is a seconds/nanoseconds value so not directly related to time zones or times offsets, its toString returns a date and time formatted as a UTC date/time, eg 2014-05-13T20:05:08.556Z. Also anInstant.atZone(zone) and anInstant.atOffset(offset) both produce a value that is consistent with treating the Instant as having an implied UTC time-zone/'zero' offset. I would have expected therefore: ZoneOffset.from(anInstant) to produce a 'zero' ZoneOffset OffsetDateTime.from(anInstant)

Deserializing LocalDateTime with Jackson JSR310 module

随声附和 提交于 2019-11-30 08:00:24
问题 I'm using the library described the Jackson Datatype JSR310 page but I'm still having difficulty getting it to work. I have configured the following bean: @Bean @Primary public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JSR310Module()); return mapper; } When I call my REST API the date format output is yyyy-MM-dd'T'HH:ss.SSSSSS , e.g. 2015-04-11T00:10:38.905847 . This gets handled by my AngularJS code just fine. When I want to submit

UnsupportedOperationException - Why can't you call toInstant() on a java.sql.Date?

拜拜、爱过 提交于 2019-11-30 07:49:48
The java.util.Date class has a method called toInstant() that converts the Date instance to a java.time.Instant . The java.sql.Date class extends the java.util.Date class, but when I attempt to call toInstant() on a java.sql.Date , I receive an UnsupportedOperationException . Why is toInstant() an unsupported operation on java.sql.Date ? And what is the "correct" way to convert a java.sql.Date to a java.time.Instant ? Check the JavaDoc Since sql.Date does not have a time component, there is no possibility to convert it to time.Instant This method always throws an UnsupportedOperationException

Java 8 Date API vs Calendar / Date / DateFormat

倾然丶 夕夏残阳落幕 提交于 2019-11-30 07:08:53
问题 There is this new-n-cool Date API in Java 8, the java.time package. I understand that it is better designed, less ambiguous and more thread-safe than the old classes. Still, I am not sure how to go about using it: Should I use it exclusively instead of the old classes? Should I replace existing usages of old classes whereever I spot them because the new stuff is so much better? Should I refrain from using java.util.Calendar, java.util.Date , java.sql.Date and java.text.DateFormat in favor of

How to convert java.sql.timestamp to LocalDate (java8) java.time?

孤街浪徒 提交于 2019-11-30 06:23:22
问题 In Java 8, how can I convert a Timestamp (in java.sql ) to a LocalDate (in java.time )? 回答1: You can do: timeStamp.toLocalDateTime().toLocalDate(); Note that timestamp.toLocalDateTime() will use the Clock.systemDefaultZone() time zone to make the conversion. This may or may not be what you want. 回答2: I'll slightly expand @assylias answer to take time zone into account. There are at least two ways to get LocalDateTime for specific time zone. You can use setDefault time zone for whole

Java8 java.util.Date conversion to java.time.ZonedDateTime

こ雲淡風輕ζ 提交于 2019-11-30 06:20:59
问题 I am getting the following exception while trying to convert java.util.Date to java.time.LocalDate . java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: 2014-08-19T05:28:16.768Z of type java.time.Instant The code is as follow: public static Date getNearestQuarterStartDate(Date calculateFromDate){ int[] quaterStartMonths={1,4,7,10}; Date startDate=null; ZonedDateTime d=ZonedDateTime.from(calculateFromDate.toInstant()); int frmDateMonth=d.getMonth().getValue(); Is

Failed to parse single digit hour and lowercase am-pm of day into Java 8 LocalTime

青春壹個敷衍的年華 提交于 2019-11-30 05:39:57
问题 When I try to run the following code: LocalTime test = LocalTime.parse("8:00am", DateTimeFormatter.ofPattern("hh:mma")); I get this: Exception in thread "main" java.time.format.DateTimeParseException: Text '8:00am' could not be parsed at index 0 Any idea why this might be happening? 回答1: The AM/PM tokens have* to be uppercase: LocalTime test = LocalTime.parse("8:00AM", DateTimeFormatter.ofPattern("hh:mma")); Patterns definitions for hours: Symbol Meaning Presentation Examples ------ ---------

Why my pattern(“yyyyMM”) cannot parse with DateTimeFormatter (java 8)

ぐ巨炮叔叔 提交于 2019-11-30 05:39:28
问题 When I using SimpleDateFormat , it can parse. SimpleDateFormat format = new SimpleDateFormat("yyyyMM"); format.setLenient(false); Date d = format.parse(date); But When I use Java 8 DateTimeFormatter , DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM"); LocalDate localDate = LocalDate.parse(date, formatter); it throws java.time.format.DateTimeParseException: Text '201510' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2015, MonthOfYear=10},ISO of

Java 8 date-time: get start of day from ZonedDateTime

你说的曾经没有我的故事 提交于 2019-11-30 04:10:33
Is there any difference between these: zonedDateTime.truncatedTo(ChronoUnit.DAYS); zonedDateTime.toLocalDate().atStartOfDay(zonedDateTime.getZone()); Any reason to prefer one against the other? Thanks Updated for sake of correction: In most cases yes the same , see following example for Brazil when switching from winter to summer time: ZonedDateTime zdt = ZonedDateTime.of(2015, 10, 18, 0, 30, 0, 0, ZoneId.of("America/Sao_Paulo")); // switch to summer time ZonedDateTime zdt1 = zdt.truncatedTo(ChronoUnit.DAYS); ZonedDateTime zdt2 = zdt.toLocalDate().atStartOfDay(zdt.getZone()); System.out