java-time

How to get DayOfWeek from an Instant.now()

走远了吗. 提交于 2020-07-10 05:36:21
问题 Assuming the following code... Instant x = Instant.now(); How do I get day of week from x? 回答1: You have to convert it to ZonedDateTime Instant.now().atZone(ZoneId.systemDefault()).getDayOfWeek() 回答2: I have awarded points to techtabu, but I ended up using atOffset instead. Here is where I ended up... int currentDayOfWeekValue = Instant.now().atOffset(ZoneOffset.UTC).getDayOfWeek().getValue(); I am amazed how difficult the Java8 datetime libraries are. There are so many variations of similar

How to query JPA LocalDateTime field with a LocalDate value?

杀马特。学长 韩版系。学妹 提交于 2020-07-09 11:52:25
问题 I search for a way to get a list of Objects created on a certain LocalDateTime date saved in the Postgresql database in a field of type TIMESTAMPTZ. To do so, I tried to use JpaRepository: List<Object> findByCreationDate(LocalDate date); But I get the error: java.lang.IllegalArgumentException: Parameter value [2020-12-12] did not match expected type [java.time.LocalDateTime (n/a)] I also tried writing the query myself with the same result. The solutions I thought about so far : To get all the

Java 8 optional time part not working

流过昼夜 提交于 2020-07-04 12:00:51
问题 I am trying to create date time format for optional time part currently I implemented this import java.time.*; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.text.ParseException; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { System.out.println(Ideone.getDate("2017-07-01T00:00:00.0Z")); System.out.println(Ideone.getDate("2017-07

How can you make LocalDate to return Date in a specific format?

♀尐吖头ヾ 提交于 2020-06-29 06:21:49
问题 How can you make LocalDate to return Date in a specific format ? Ex . LocalDate ld = LocalDate.now(); The above statement will return date like 2018-11-24 but i want it to return like 24-11-2018 . ** Dont say use formatter because formatter will not return date , it will return String which is of no use for me . 回答1: Your question is contradictory, for example you want "24-11-2018" but also want "date" instead of "String" and overlook the fact that "24-11-2018" is a date in string form. A

How can you make LocalDate to return Date in a specific format?

我的未来我决定 提交于 2020-06-29 06:21:10
问题 How can you make LocalDate to return Date in a specific format ? Ex . LocalDate ld = LocalDate.now(); The above statement will return date like 2018-11-24 but i want it to return like 24-11-2018 . ** Dont say use formatter because formatter will not return date , it will return String which is of no use for me . 回答1: Your question is contradictory, for example you want "24-11-2018" but also want "date" instead of "String" and overlook the fact that "24-11-2018" is a date in string form. A

Moshi LocalDateTime adapter with multiple format

别来无恙 提交于 2020-06-24 22:28:39
问题 By default, ThreeTenABP.LocalDateTime is converted to {"date":{"day":10,"month":4,"year":2018},"time":{"hour":3,"minute":34,"nano":115000000,"second":18}} I can write an adapter to support ISO date string 2018-04-10T03:45:26.009 class LocalDateTimeAdapter { @ToJson fun toJson(value: LocalDateTime): String { return FORMATTER.format(value) } @FromJson fun fromJson(value: String): LocalDateTime { return FORMATTER.parse(value, LocalDateTime.FROM) } companion object { private val FORMATTER =

Java 8 DateTimeFormatter for month in all CAPS not working [duplicate]

橙三吉。 提交于 2020-06-23 20:32:45
问题 This question already has an answer here : How to handle upper or lower case in JSR 310? [duplicate] (1 answer) Closed 4 years ago . I'm trying to parse date time string to LocalDateTime. However if I send month with all caps its thorwning an error, is there any workaround. Here is the below code @Test public void testDateFormat(){ DateTimeFormatter formatter= DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse("04-NOV-2015 16:00:00", formatter);

DateTimeFormatter pattern with liternal and no separator does not work

雨燕双飞 提交于 2020-06-23 06:13:32
问题 The parser generated by DateTimeFormatter.ofPattern exhibits the following interesting behaviour which is preventing me from writing a pattern to parse a string like 20150100 : System.out.println(DateTimeFormatter.ofPattern("yyyyMM").parse("201501", YearMonth::from)); // works System.out.println(DateTimeFormatter.ofPattern("yyyyMM'aa'").parse("201501aa", YearMonth::from)); // works System.out.println(DateTimeFormatter.ofPattern("yyyyMM'00'").parse("20150100", YearMonth::from)); // java.time

DateTimeFormatter pattern with liternal and no separator does not work

孤街浪徒 提交于 2020-06-23 06:13:18
问题 The parser generated by DateTimeFormatter.ofPattern exhibits the following interesting behaviour which is preventing me from writing a pattern to parse a string like 20150100 : System.out.println(DateTimeFormatter.ofPattern("yyyyMM").parse("201501", YearMonth::from)); // works System.out.println(DateTimeFormatter.ofPattern("yyyyMM'aa'").parse("201501aa", YearMonth::from)); // works System.out.println(DateTimeFormatter.ofPattern("yyyyMM'00'").parse("20150100", YearMonth::from)); // java.time

Getting the the current Instant in a specific TimeZone

亡梦爱人 提交于 2020-06-22 12:37:07
问题 I have tried to get the current Instance on a specific timeZone but it does not work as expected. Any idea on how to do this ? Here is what i did: Instant.now(Clock.system(ZoneId.of("America/Los_Angeles"))).truncatedTo(ChronoUnit.SECONDS) However the instant time returned is always UTC. I changed many time the ZoneID and it is always wrong. Please advise. EDIT: I'm interacting with an application that generate log with timeStamp and i need to operate over those event. For instant if I start