java-time

Confusion in java.Clock, systemDefaultZone() returning UTC time

≯℡__Kan透↙ 提交于 2019-12-24 06:54:09
问题 I am trying to understand why the following java.time.Clock is returning UTC time instead of the local time zone (EST). C:\Users\Felipe>scala Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65). Type in expressions for evaluation. Or try :help. scala> import java.time._ import java.time._ scala> ZoneId.systemDefault() res0: java.time.ZoneId = America/New_York scala> val clock = Clock.systemDefaultZone() clock: java.time.Clock = SystemClock[America/New_York] scala> clock

Confusion in java.Clock, systemDefaultZone() returning UTC time

假如想象 提交于 2019-12-24 06:53:48
问题 I am trying to understand why the following java.time.Clock is returning UTC time instead of the local time zone (EST). C:\Users\Felipe>scala Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65). Type in expressions for evaluation. Or try :help. scala> import java.time._ import java.time._ scala> ZoneId.systemDefault() res0: java.time.ZoneId = America/New_York scala> val clock = Clock.systemDefaultZone() clock: java.time.Clock = SystemClock[America/New_York] scala> clock

How to parse string with date, but without time in local format to ZonedDateTime?

孤者浪人 提交于 2019-12-24 02:43:15
问题 This question is similar to How to parse ZonedDateTime with default zone? but addinitional condition. I have a string param that represent a date in UK format: "3/6/09". It doesn't contain time, only date. But may contain it and even time zone. And I want to parse it to ZonedDateTime . public static ZonedDateTime parse(String value) { DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(SHORT).withLocale(Locale.UK).withZone(ZoneId.systemDefault()); TemporalAccessor

How to get the data time pattern with time zone based on the locale?

痞子三分冷 提交于 2019-12-24 02:16:04
问题 The following code is what I already have: DateFormat f = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Java_Locale); SimpleDateFormat sf = (SimpleDateFormat) f; String pattern = sf.toPattern(); With the above code, I am able to get the PROPER date/time pattern based on the locale. For example: "M/d/yy h:mm a" for US English, "yy-M-d ah:mm" for Chinese. However, this pattern does not have the time zone information. I hope to be able to add time zone into the pattern. For

Java: How to check whether given time lies between two times?

不羁岁月 提交于 2019-12-23 04:54:12
问题 I want to check whether target time lies between two given times without considering date using Java8 time. Let say if starting time is "21:30" , ending time is "06:30" and target time is "03:00" , so program should return true. @Test public void posteNuit() { DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm"); String s = "21:30"; String e = "06:30"; String t = "03:00"; LocalTime startTime = LocalTime.parse(s, format); LocalTime endTime = LocalTime.parse(e, format); LocalTime

Importance of order when using multiple optional patterns

╄→尐↘猪︶ㄣ 提交于 2019-12-23 01:27:33
问题 How does the order of optional patterns in a DateTimeFormatter affect the parsing operation? I was running this program and wondered why the last line throws an exception but not the first three. public static void main(String[] args) { String p1 = "[EEEE][E] dd-MM-yyyy"; String p2 = "[E][EEEE] dd-MM-yyyy"; String date1 = "Thu 07-01-2016"; String date2 = "Thursday 07-01-2016"; parse(date1, p1); //OK parse(date1, p2); //OK parse(date2, p1); //OK parse(date2, p2); //Exception } private static

java.time YearMonth as a type within entity

随声附和 提交于 2019-12-22 12:25:35
问题 How do you use YearMonth within an entity? I have seen a lot of stack overflow answers about a converter? Is this still needed and if so how? @Basic(optional = false) @NotNull @Column(name = "cc_exp_date") private YearMonth ccExpDate; The exception I am getting is below. Could I just convert this into a date within the set method and convert back to YearMonth within the get method of the entity as a work around? MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr

Java 1.8 time functionality

故事扮演 提交于 2019-12-22 10:48:55
问题 So Java 1.8 comes with a whole new (and old) bunch of classes to manage time calculations: java.time.Instant , java.time.LocalTime , java.time.temporal.ChronoUnit , and maybe more... But why is there no simple way to calculate the time difference between any of these? I would expect "time_later - time_earlier" to be the most used manipulation of time, but this is nowhere to be seen. I cannot subtract one LocalTime from the other and get a new LocalTime; I cannot subtract one Instant from the

Convert XMLGregorianCalendar in GMT to LocalDateTime Pacific time

会有一股神秘感。 提交于 2019-12-22 09:32:13
问题 I'm trying to convert XMLGregorianCalendar which is sent in GMT/UTC format to Java 8 LocalDateTime in America/Los_Angeles timezone with no luck. Here is what I tried and couldn't get the time converted to Pacific time. //xmlDate is 2017-11-13T00:00:00Z ZonedDateTime zDateTime = xmlDate.toGregorianCalendar().toZonedDateTime().toLocalDateTime().atZone(ZoneId.of("America/Los_Angeles")); LocalDateTime localDateTime = zDateTime.toLocalDateTime(); //Expected localDateTime is 2017-11-12T16:00. But I

What is the equivalent format string of DateTimeFormatter.ISO_OFFSET_DATE_TIME?

可紊 提交于 2019-12-22 08:29:34
问题 Do we know if there is an equivalent format string that outputs the same result as DateTimeFormatter.ISO_OFFSET_DATE_TIME ? i.e. ZonedDateTime dateTime = ZonedDateTime.now(); System.out.println(dateTime.format(DateTimeFormatter.ofPattern(pattern))); System.out.println(dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); would output the same 回答1: No, this is not possible On request: Yes, we know that there is no equivalent format pattern string of DateTimeFormatter.ISO_OFFSET_DATE_TIME .