java-time

String to OffsetDateTime parse in java

和自甴很熟 提交于 2019-12-11 17:11:06
问题 I am trying to parse date string to OffsetDateTime as below. But I am getting below exception, Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon Jun 18 00:00:00 IST 2012' could not be parsed at index 0 public class ParseExample { public static void main(String... args) throws ParseException { String dateStr = "Mon Jun 18 00:00:00 IST 2012"; System.out.println(OffsetDateTime.parse(dateStr)); } } can someone please help me with this mistake. Thanks. 回答1:

Using Collections.binarySearch() for predicate search (i.e., not complete match)

早过忘川 提交于 2019-12-11 15:15:00
问题 I have a list of timestamps sorted in ascending order: List<Instant> timestamps = ...; // note: sorted in ascending order Now, given an input timestamp Instant inputTs , I want to find an entry t in timestamps that satisfies t.isBefore(inputTs) && inputTs.isBefore(t.plusMillis(SOME_CONSTANT)) , i.e., I am simply looking for a t such that inputTs lies within the bounds of some fixed-length duration starting at t . I acknowledge that there can theoretically be multiple such t s, so the search

How to get Date in UTC+0 in Java?

一曲冷凌霜 提交于 2019-12-11 07:37:43
问题 I am using the following code to get the date in ISO-8601 format. For UTC the value returned does not contain offset. OffsetDateTime dateTime = OffsetDateTime.ofInstant( Instant.ofEpochMilli(epochInMilliSec), zoneId); return dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); For other time formats the response returned looks like: 2016-10-30T17:00:00-07:00 In case of UTC the value returned is: 2016-10-30T17:00:00Z I want it to be: 2016-10-30T17:00:00+00:00 Note: Do not use UTC-0 as -00

DateTimeFormatter to parse all ISO-valid styles of zone-offset [duplicate]

北城余情 提交于 2019-12-11 07:33:16
问题 This question already has answers here : Java 8 Date and Time: parse ISO 8601 string without colon in offset [duplicate] (4 answers) Closed 8 months ago . With the following formatter I'm able to parse "2017-03-28T23:40:06.000+0100" new DateTimeFormatterBuilder() .append(ISO_LOCAL_DATE_TIME) .appendPattern("X") .toFormatter(); With another one it parses "2017-03-28T23:40:06.000+01:00" new DateTimeFormatterBuilder() .append(ISO_LOCAL_DATE_TIME) .appendPattern("XX") .toFormatter(); However, I'm

`DateTimeFormatter` to handle either a FULL STOP or COMMA in fractional second

自作多情 提交于 2019-12-11 06:40:59
问题 In the java.time classes, how do we specify a formatting pattern for a DateTimeFormatter that allows for either a FULL STOP ( . ) (period or dot) or a COMMA ( , ) as the delimiter of a fractional second? For example, the following works for parsing an input date value ending in either .0Z or ,0Z in 20090813145607.0Z and 20090813145607,0Z . String input = "20090813145607.0Z"; DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuuMMddHHmmss[,S][.S]X" ); But for printing, the output contains

Parse String for unknown TemporalAccessor

六眼飞鱼酱① 提交于 2019-12-11 06:26:40
问题 I'm getting a request parameter with unknown temporal (date, time or timestamp in ISO format) and want to parse it into a java.time.temporal.TemporalAccessor : LocalDate when the string represents a date like "2018-02-28" or LocalDateTime when the string represents a timestamp like "2018-02-28T11:20:00" The following attempt results into DateTimeParseException : TemporalAccessor dt = DateTimeFormatter.ISO_DATE_TIME.parseBest(str, LocalDateTime::from, LocalDate::from); Deciding on the length

Is java.time failing to parse fraction-of-second?

两盒软妹~` 提交于 2019-12-11 06:09:13
问题 With the first release of Java 8 (b132) on Mac OS X (Mavericks), this code using the new java.time package works: String input = "20111203123456"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss"); LocalDateTime localDateTime = LocalDateTime.parse( input, formatter ); Rendering: 2011-12-03T12:34:56 But when I add "SS" for fraction-of-second (and "55" as input), as specified in the DateTimeFormatter class doc, an exception is thrown: java.time.format

Java 8 documentation Date-time tutorials mistake

╄→尐↘猪︶ㄣ 提交于 2019-12-11 03:52:12
问题 The Oracle Tutorial page for the Temporal Query show this example code. - Code TemporalQueries query = TemporalQueries.precision(); System.out.printf("LocalDate precision is %s%n",LocalDate.now().query(query)); When I compile this segment code, the Compiler throws the error: - Error TemporalQueryExample.java:8: error: incompatible types: TemporalQuery<TemporalUnit> cannot be converted to TemporalQueries TemporalQueries query = TemporalQueries.precision(); ^ TemporalQueryExample.java:10: error

Implicit parse LocalDate with Post @Consumes(MediaType.ApplicationJson)

半腔热情 提交于 2019-12-11 02:36:51
问题 this is my Endpoint in which I would like to add a "Firma" with a post request, but the JSON cannot implicit parse the timestamp . @POST @Consumes(MediaType.APPLICATION_JSON) public Response addFirma(Firma firma){ firmaFacade.create(firma); return Response.ok().build(); } Those are the variables of "Firma" private int firm1; private LocalDate firm2; And this is the JSON-String I sent - LocalDate is NULL { "firm1":2, "firm2":"2017-09-09" } But whenever I use the get Request with test data, it

How to convert a given time (String) to a LocalTime?

喜夏-厌秋 提交于 2019-12-10 22:59:30
问题 I will be asking a user to enter a specific time: 10AM, 12:30PM, 2:47PM, 1:09AM, 5PM, etc. I will be using a Scanner to get the user's input. How can I parse/convert that String to a LocalTime object? Is there any built-in function in Java that will allow me to do that? 回答1: Just use a java.time.format.DateTimeFormatter : DateTimeFormatter parser = DateTimeFormatter.ofPattern("h[:mm]a"); LocalTime localTime = LocalTime.parse("10AM", parser); Explaining the pattern: h : am/pm hour of day (from