java-time

configure a custom variant in HijrahChronology for date correction jdk 8

拥有回忆 提交于 2020-01-02 09:58:27
问题 I have used DatePicker in javafx - JDK 8 and used HijrahChronology.INSTANCE - so that the date picker shows both the calendar - everything work good enough but I am having a difference of 1 day between gregorian calendar and hijri calendar. Hijri Calendar is 1 day backward. I am trying to change the variant as per the following link https://bugs.openjdk.java.net/browse/JDK-8187987 but unable to succeed in it. Kindly explain or refer a better or a clear solution to this issue. Code:

Coverting String to LocalTime with/without nanoOfSeconds

时间秒杀一切 提交于 2020-01-02 01:06:06
问题 I need to convert a string to LocalTime (java-8 not joda) that may or maynot have nanoOfSeconds in the string. The String format is in the form of 07:06:05 or 07:06:05.123456 The string may or may not have a decimal place in the seconds and when it does there could be any number of characters to represent the Nano Seconds part. Using a DateTimeForamtter such as DateTimeFormatter dtf = DateTimeFormatter.ofPattern("H:mm:ss"); or DateTimeFormatter dtf = DateTimeFormatter.ofPattern("H:mm:ss

How to avoid negative time between time difference?

☆樱花仙子☆ 提交于 2020-01-01 19:16:13
问题 I'm developing an app in which I'm using Java8 Time. I'm facing an issue. Let's say Time A is 08:00 and Time B is 17:00, so the difference of between these two times will be 9h which in my case is correct, but if Time A is 18:00 and Time B is 02:00 it should be 8h, but in my case my program is returning -16. Kindly someone guide me how to solve this. My code: @Test public void testTime() { DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm"); String s = "18:00"; String e = "02:00";

java.time equivalent of Joda-Time `withTimeAtStartOfDay`? (get first moment of the day)

一笑奈何 提交于 2020-01-01 08:19:05
问题 In the Joda-Time library, the DateTime class offers a method withTimeAtStartOfDay to get the first moment of the day. You might think of that moment as "midnight". That first moment is usually the time 00:00:00.000 but not always. Does the java.time package found in Java 8 and later have an equivalent feature? 回答1: The equivalent is using a special method, atStartOfDay, in the class LocalDate: ZoneId zoneId = ZoneId.of("America/New_York"); ZonedDateTime zdt = LocalDate.now(zoneId)

java.time equivalent of Joda-Time `withTimeAtStartOfDay`? (get first moment of the day)

放肆的年华 提交于 2020-01-01 08:19:04
问题 In the Joda-Time library, the DateTime class offers a method withTimeAtStartOfDay to get the first moment of the day. You might think of that moment as "midnight". That first moment is usually the time 00:00:00.000 but not always. Does the java.time package found in Java 8 and later have an equivalent feature? 回答1: The equivalent is using a special method, atStartOfDay, in the class LocalDate: ZoneId zoneId = ZoneId.of("America/New_York"); ZonedDateTime zdt = LocalDate.now(zoneId)

Format a date using the new date time API

自闭症网瘾萝莉.ら 提交于 2019-12-31 17:38:12
问题 I was playing with the new date time API but when running this: public class Test { public static void main(String[] args){ String dateFormatted = LocalDate.now() .format(DateTimeFormatter .ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println(dateFormatted); } } It throws: Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay at java.time.LocalDate.get0(LocalDate.java:680) at java.time.LocalDate.getLong(LocalDate.java:659) at java.time

How to parse different ISO date/time formats with Jackson and java.time?

佐手、 提交于 2019-12-30 14:47:28
问题 Our Rest API takes JSON input from several external parties. They all use "ISO-ish" formats, but the formatting of the time zone offset is slightly different. These are some of the most common formats we see: 2018-01-01T15:56:31.410Z 2018-01-01T15:56:31.41Z 2018-01-01T15:56:31Z 2018-01-01T15:56:31+00:00 2018-01-01T15:56:31+0000 2018-01-01T15:56:31+00 Our stack is Spring Boot 2.0 with Jackson ObjectMapper. In our data classes we use the type java.time.OffsetDateTime a lot. Several developers

Parsing date and AM/PM using java 8 DateTime API

僤鯓⒐⒋嵵緔 提交于 2019-12-30 08:08:14
问题 I'm trying to parse a date time string using java 8 DateTime API. The string to parse contains date, month, year and AM/PM marker such as 17/02/2015 PM . I use the following pattern: dd/MM/yyyy aa and I expect the parsed LocalDateTime time part to be set at 12:00:00 or 00:00:00 depending on the AM/PM marker. Using the former java.text.SimpleDateFormat API, the parsing works as expected using this pattern. But when i use java 8 DateTime API and run the following code: LocalDateTime.parse("17

Parse clock time in java 8

安稳与你 提交于 2019-12-29 08:09:12
问题 I wonder is it possible to parse clock time hour:minute:second in Java 8? e.g. final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); final String str = "12:22:10"; final LocalDateTime dateTime = LocalDateTime.parse(str, formatter); I tried but get this exception: Exception in thread "main" java.time.format.DateTimeParseException: Text '12:22:10' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 12:22:10 of type java.time

Java Time's week-of-week-based-year pattern parsing with DateTimeFormatter

帅比萌擦擦* 提交于 2019-12-29 07:56:08
问题 I need to output the current date in the format week-based-year - week-of-week-based-year , i.e. using the ISO week date where the week always starts on a Monday and the first week of the year is the first one that has at least four days in January (so the week with the first Thursday in January). Since 31 December 2015 was a Thursday, the Friday through Sunday, i.e. 1st through 3rd of January 2016, all belong in the 53rd week of 2015 (a "long year"), and the first week of 2016 starts on