java-time

How can I mock java.time.LocalDate.now()

二次信任 提交于 2020-01-09 04:22:50
问题 In my test case, I need test time sensitive method, in that method we're using java 8 class LocalDate, it is not Joda . What can I do to change time, when I'm running test 回答1: In your code, replace LocalDate.now() with LocalDate.now(clock);. You can then pass Clock.systemDefaultZone() for production and a fixed clock for testing. This is an example : First, inject the Clock . If you are using spring boot just do a : @Bean public Clock clock() { return Clock.systemDefaultZone(); } Second,

Parse time of day string

五迷三道 提交于 2020-01-06 14:07:10
问题 Input is a string such as "23:55" or "09:20", i.e. in the "HH:mm" format. I want the string to specify the time of day in UTC. I want to use java.time to parse the string and the output to be an Instant with the specified time of day today. If needed I can append a time zone to the string, i.e. "23:55 UTC". I want to use the resulting instant to calculate the number of nanos between now and the mentioned instant. I.e. Duration.between(Instant.now(), resultingInstant).toNanos(); Edit: Might

java.time.LocalDate.getDayOfWeek() to java.util.Calendar.get(DAY_OF_WEEK)

那年仲夏 提交于 2020-01-06 13:31:59
问题 I am using the recent java.time package and in particular the day of week getter of LocalDate. I get a nice DayOfWeek enum when calling LocalDate.getDayOfWeek() . However, my code should be compatible with some older part of the application that uses the integer value of a Calendar weekday, i.e. obtained from the code Calendar.get(DAY_OF_WEEK) . Obviously, in the DayOfWeek enum, SUNDAY value is 7, and in Calendar DAY_OF_WEEK field, SUNDAY value is 1. So, what choices do I have to convert one

java.time.LocalDate.getDayOfWeek() to java.util.Calendar.get(DAY_OF_WEEK)

梦想与她 提交于 2020-01-06 13:31:26
问题 I am using the recent java.time package and in particular the day of week getter of LocalDate. I get a nice DayOfWeek enum when calling LocalDate.getDayOfWeek() . However, my code should be compatible with some older part of the application that uses the integer value of a Calendar weekday, i.e. obtained from the code Calendar.get(DAY_OF_WEEK) . Obviously, in the DayOfWeek enum, SUNDAY value is 7, and in Calendar DAY_OF_WEEK field, SUNDAY value is 1. So, what choices do I have to convert one

Jackson Java 8 DateTime serialisation

▼魔方 西西 提交于 2020-01-04 17:51:10
问题 Jackson operates java.time.Instant with WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS ( READ_ as well) enabled by default. jackson-datatype-jsr310 It produces JSON like this { "createDate":1421261297.356000000, "modifyDate":1421261297.356000000 } In JavaScript it's much easier to get Date from traditional millis timestamp (not from seconds/nanos as above), like new Date(1421261297356) . I think there should be some reason to have the nanos approach by default, so what is that reason? 回答1: One way is

Mapstruct LocalDateTime to Instant

跟風遠走 提交于 2020-01-03 20:59:46
问题 I am new in Mapstruct. I have a model object which includes LocalDateTime type field. DTO includes Instant type field. I want to map LocalDateTime type field to Instant type field. I have TimeZone instance of incoming requests. Manually field setting like that; set( LocalDateTime.ofInstant(x.getStartDate(), timeZone.toZoneId()) ) How can I map these fields using with Mapstruct? 回答1: You have 2 options to achieve what you are looking for. First option: Use the new @Context annotation from 1.2

Parse date-only as LocalDateTime in Java 8

眉间皱痕 提交于 2020-01-03 17:35:53
问题 I need to parse a field which is sometimes given as a date and sometimes as a date/time. Is it possible to use single datatype for this using Java 8 time API? Currently, I attempted to use a LocalDateTime for it, but for following invocation LocalDateTime.parse("1986-04-08", DateTimeFormatter.ofPattern("yyyy-MM-dd")) I get a java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 1986-04-08 of type java.time.format.Parsed This is part of some

Parse date-only as LocalDateTime in Java 8

懵懂的女人 提交于 2020-01-03 17:32:14
问题 I need to parse a field which is sometimes given as a date and sometimes as a date/time. Is it possible to use single datatype for this using Java 8 time API? Currently, I attempted to use a LocalDateTime for it, but for following invocation LocalDateTime.parse("1986-04-08", DateTimeFormatter.ofPattern("yyyy-MM-dd")) I get a java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 1986-04-08 of type java.time.format.Parsed This is part of some

Localize string from `MonthDay` or `YearMonth` classes in java.time? [duplicate]

非 Y 不嫁゛ 提交于 2020-01-03 02:52:32
问题 This question already has answers here : How to format YearMonth and MonthDay depending on a Locale? (3 answers) Closed 2 years ago . The java.time classes built into Java 8 and later offer the MonthDay and YearMonth classes. Their toString and parse methods use standard ISO 8601 formats ( --MM-DD & YYYY-MM ), which is wise. For presentation to humans, the standard formats may not be suitable. Is there anyway to generate an automatically localized string to represent the values in objects of

Localize string from `MonthDay` or `YearMonth` classes in java.time? [duplicate]

别说谁变了你拦得住时间么 提交于 2020-01-03 02:52:00
问题 This question already has answers here : How to format YearMonth and MonthDay depending on a Locale? (3 answers) Closed 2 years ago . The java.time classes built into Java 8 and later offer the MonthDay and YearMonth classes. Their toString and parse methods use standard ISO 8601 formats ( --MM-DD & YYYY-MM ), which is wise. For presentation to humans, the standard formats may not be suitable. Is there anyway to generate an automatically localized string to represent the values in objects of