java-time

How can I return LocalDate.now() in milliseconds?

a 夏天 提交于 2020-06-22 09:35:21
问题 I create date now: ZoneId gmt = ZoneId.of("GMT"); LocalDateTime localDateTime = LocalDateTime.now(); LocalDate localDateNow = localDateTime.toLocalDate(); Then I want return this date in milliseconds: localDateNow.atStartOfDay(gmt) - 22.08.2017 localDateNow.atStartOfDay(gmt).toEpochSecond(); - 1503360000 (18.01.70) How can I return LocalDate.now() in milliseconds? 回答1: Calling toInstant().toEpochMilli() , as suggested by @JB Nizet's comment, is the right answer, but there's a little and

How to query LocalDateTime with LocalDate?

被刻印的时光 ゝ 提交于 2020-06-16 04:56:30
问题 I've got a class which contains an atttribute of java.time.LocalDateTime type. public class MyClass{ // ... private LocalDateTime fecha; // ... } I'm using Spring Data repositories. What I want to accomplish is to query entities according to a date: @Service public interface IRepository extends CrudRepository<MyClass, UUID> { // ... public void deleteByFecha(LocalDate fecha); // ... } But this does not work, as an exception is thrown: org.springframework.dao.InvalidDataAccessApiUsageException

Create Java DateTime Instant from microseconds

心不动则不痛 提交于 2020-06-13 07:23:48
问题 There has been changes in Java Date & Time API Since Java 9. LocalDateTime now has microseconds precision. Java 9 has a fresh implementation of java.time.Clock capable of capturing the current moment in resolution finer than milliseconds (three digits of decimal fraction). We get the time in microseconds from our backend service. System.currentTimeMillis > 1565245051795 > 2019-08-08T06:17:31.795 Service.getTime > 1565245051795306 > 2019-08-08T06:17:31.795306 In order to construct a

DateTimeFormatter weekday seems off by one

こ雲淡風輕ζ 提交于 2020-06-10 02:35:08
问题 I'm porting an existing application from Joda-Time to Java 8 java.time . I ran into a problem where parsing a date/time string that contains a 'day of week' value triggered an exception in my unit tests. When parsing: 2016-12-21 20:50:25 Wednesday December +0000 3 using format: yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'EEEE' 'MMMM' 'ZZ' 'e I get: java.time.format.DateTimeParseException: Text '2016-12-21 20:50:25 Wednesday December +0000 3' could not be parsed: Conflict found: Field DayOfWeek 3 differs

How to format a ZonedDateTime to a String?

北慕城南 提交于 2020-06-06 04:45:52
问题 I want to convert a ZonedDateTime to a String in the format of ("dd/MM/yyyy - hh:mm") . I know this is possible in Joda-Time other types, just using their toString("dd/MM/yyyy - hh:mm") ....But this doesn't work with ZonedDateTime.toString() . How can I format a ZonedDateTime to a String ? EDIT: I tried to print the time in another timezone and the result appears to be the same always: ZonedDateTime date = ZonedDateTime.now(); ZoneId la = ZoneId.of("America/Los_Angeles"); ZonedDateTime date2

How to format a ZonedDateTime to a String?

社会主义新天地 提交于 2020-06-06 04:44:30
问题 I want to convert a ZonedDateTime to a String in the format of ("dd/MM/yyyy - hh:mm") . I know this is possible in Joda-Time other types, just using their toString("dd/MM/yyyy - hh:mm") ....But this doesn't work with ZonedDateTime.toString() . How can I format a ZonedDateTime to a String ? EDIT: I tried to print the time in another timezone and the result appears to be the same always: ZonedDateTime date = ZonedDateTime.now(); ZoneId la = ZoneId.of("America/Los_Angeles"); ZonedDateTime date2

How to format a ZonedDateTime to a String?

纵然是瞬间 提交于 2020-06-06 04:44:12
问题 I want to convert a ZonedDateTime to a String in the format of ("dd/MM/yyyy - hh:mm") . I know this is possible in Joda-Time other types, just using their toString("dd/MM/yyyy - hh:mm") ....But this doesn't work with ZonedDateTime.toString() . How can I format a ZonedDateTime to a String ? EDIT: I tried to print the time in another timezone and the result appears to be the same always: ZonedDateTime date = ZonedDateTime.now(); ZoneId la = ZoneId.of("America/Los_Angeles"); ZonedDateTime date2

Need to get correct output for number of weeks in a month irrespective of what date format being used

六月ゝ 毕业季﹏ 提交于 2020-06-01 06:24:05
问题 I have this code which returns Number of week correctly. package org.test.Calendar; import java.util.Calendar; import java.util.GregorianCalendar; public class GetDaysInMonth { public static void main(String[] args) { Calendar calendar = GregorianCalendar.getInstance(); int year = 2020; int month = Calendar.MAY; int date = 1; calendar.set(year, month, date); int numOfDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK

Get the next LocalDateTime for a given day of week

℡╲_俬逩灬. 提交于 2020-05-23 06:01:24
问题 I want to create instance of LocalDateTime at the date/time of the next (for example) Monday. Is there any method in Java Time API, or should I make calculations how many days are between current and destination dates and then use LocalDateTime.of() method? 回答1: There is no need to do any calculations by hand. You can adjust a given date with an adjuster with the method LocalDateTime.with(adjuster). There is a built-in adjuster for the next day of the week: TemporalAdjusters.next(dayOfWeek):

Android - Not able to get microseconds using java.time.Instant

浪子不回头ぞ 提交于 2020-05-14 10:14:33
问题 I am unable to get the microseconds using java.time.Instant (Instant.now()) from android. I am getting the date time without microseconds like 2020-04-10T12:43:40. 442Z from android But when I execute a independent java program, with the use of java.time.Instant (Instant.now()) I am getting the date time with microseconds like 2020-04-10T12:43:40. 442179Z It seems the issue is with java versions in android studio. Please help me to fix this issue. Note: I have installed Android Studio - 3.6.1