java-time

How to compare two times in java?

青春壹個敷衍的年華 提交于 2019-12-08 14:09:02
问题 I am developing a app for sync contacts. Every contact have a update_time field. I want to compare this current update_time to previous update_time . My problem is I am storing this time as string format in my database . 1.How to convert this string to time? and 2.How to compare the two times? 2014-07-11 15:10:55 this is my time format. any help or comments welcome. thank you for your valuable answers. 回答1: (You may want to consider storing the values in your database as integers instead of

Getting incorrect result when checking a date between two other dates in Java

谁说我不能喝 提交于 2019-12-08 07:55:16
问题 I'm using the code below to check if an hour is between two other specific hours: String openHour = "08:00 AM"; String currentHour = "10:00 PM"; String closeHour = "11:00 PM"; //Change to 02:00 AM doesn't work!!! SimpleDateFormat format = new SimpleDateFormat("hh:mm a"); Date openHourDate = format.parse(openHour); Date currentHourDate = format.parse(currentHour); Date closeHourDate = format.parse(closeHour); Calendar openCalendar = Calendar.getInstance(); openCalendar.setTime(openHourDate);

Define a few specific date formatters as members of an enum in Java

情到浓时终转凉″ 提交于 2019-12-08 06:49:27
问题 I need to track a few formats (DateTimeFormatter objects) for repeated use in my app. ➥ How to represent these formatter objects by using a Java enum? 回答1: tl;dr Yes. FormatDateOnly.DD_MM_YYYY.getFormatter() Details Yes, you can easily store some DateTimeFormatter objects in an enum. The slick enum facility in Java is quite powerful and flexible. Basically, an enum in Java is almost a normal Java class. Your enum can have member variables to store objects internally. Your enum can have a

Checking if LocalDateTime falls within a time range

邮差的信 提交于 2019-12-08 04:55:08
问题 I have a time A which should fall within 90 minutes range of timeB (before and after). Example: if timeB is 4:00 pm , time A should be between 2:30pm (-90) to 5:30pm (+90) Tried the following : if(timeA.isAfter(timeB.minusMinutes(90)) || timeA.isBefore(timeB.plusMinutes(90))) { return isInRange; } Can you help me whats wrong with the logic here? 回答1: As @JB Nizet said in the comments, you're using the OR operator ( || ). So you're testing if A is after B - 90 OR A is before B + 90 . If only

Grails 3 and Java 8 time support

折月煮酒 提交于 2019-12-08 01:34:07
问题 In grails 3 application, I see that new java.time classes are persisted to database. Dynamic queries and create criteria works. However, I am wondering if there is some better way how to store these time data to database. When I look to database I see some binary data, it seems it just serialize the time objects. Is there any similar plugin as joda-time-plugin or is there any way how to configure database mapping for java.time classes? 回答1: Edit : Grails 3.1.1 has hibernate 5 so this is not

How do I get POSIX time (UTC) from a serial value local date-time with the Java 8 DateTime API

▼魔方 西西 提交于 2019-12-07 23:53:04
问题 I have a timestamp that is similar to POSIX Time with the sole exception that it is not reckoned in UTC. Instead, it is the number of milliseconds that have elapsed since midnight, Jan 1 1970 in a particular local time zone . In order to make this value into an Instant , I must first know its offset (in milliseconds) to UTC/GMT. So the problem is this: knowing the local time zone id, eg. "America/Chicago" and a count of milliseconds since the local Epoch, how do I make an Instant (which must

Java Time API - A better way to get total elapsed time

*爱你&永不变心* 提交于 2019-12-07 19:55:58
问题 This is my first oportunity to play with the "new" java.time package from Java 8. I need to get the total elapsed time, something like: 1 day, 2h:3m:4s 5ms I know that have 2 TemporalAmount implementations for intervals: - Period for years, months and days - Duration for hours, minutes, seconds, milliseconds and nanoseconds There's a way to mix these two or something more straightforward than "do math"? That was the best I could do until now: (Updated with a new improved version)

Converting org.joda.time.Period to java.time.Period

会有一股神秘感。 提交于 2019-12-07 17:38:49
问题 I am trying to replace org.joda.time.Period with java.time. We have the following values stored in DB. P1M, P1Y, P1D, PT1H, PT1M Just to parse this value, Period monthly = ISOPeriodFormat.standard().parsePeriod(<One of the above value from DB>); This is simple and getting Period as expected. But, now replacing with java.time is troublesome. Because, P1D, P1M, P1Y should be parsed using the below code, java.time.Period period = java.time.Period.parse("P1M"); And, P1H and P1D should be parsed

How to handle all Zone Offset in one DateTimeFormater Java 8

Deadly 提交于 2019-12-07 13:44:47
问题 I need to create a DateTimeFormatter for the following valid dates. String date1 = "2017-06-20T17:25:28"; String date2 = "2017-06-20T17:25:28.477777"; String date3 = "2017-06-20T17:25:28.477777Z"; String date4 = "2017-06-20T17:25:28.477777UTC"; String date5 = "2017-06-20T17:25:28.477777-05"; String date6 = "2017-06-20T17:25:28.477777+05"; String date7 = "2017-06-20T17:25:28.477777+05:30"; String date8 = "2017-06-20T17:25:28.477777-05:30"; String date9 = "2017-06-20T17:25:28.477777+0530";

Getting time range between midnight and current time JDK 8

依然范特西╮ 提交于 2019-12-07 11:02:45
问题 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,