java-time

Convert number of seconds into HH:MM (without seconds) in Java

﹥>﹥吖頭↗ 提交于 2019-11-28 12:46:10
问题 I'm aware that you can use DateUtils.formatElapsedTime(seconds) to convert a number of seconds into a String with the format HH:MM:SS . But are there any utility functions that let me perform the same conversion but without the seconds? For example, I want to convert 3665 seconds into 1:01 , even though it's exactly 1:01:05 . In other words, simply dropping the seconds part. Would strongly prefer an answer that points to a utility function (if one exists) rather than a bunch of home rolled

java date parse exception while conveting UTC to local time zone

橙三吉。 提交于 2019-11-28 11:47:57
问题 I get a UTC date with following format and how can convert this UTC date to my local time zone? my input date is 2015-03-17 06:00:00 +0000 and I tried the following coding its gives parsing exception. My code: DateFormat utcFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss 'Z'"); utcFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = utcFormat.parse("2015-03-17 06:00:00 +0000"); // java.text.DateFormat.parse(Unknown Source) utcFormat.setTimeZone(TimeZone.getDefault()); System.out

Java8 DateTimeFormatter am/pm

蓝咒 提交于 2019-11-28 11:01:56
I am trying to parse some dates, but the DateTimeParser seems to disagree with me on what is valid import java.time.ZonedDateTime import java.time.format.DateTimeFormatter import java.util.Locale ZonedDateTime.parse("Wed Jul 16, 2016 4:38pm EDT", DateTimeFormatter.ofPattern("EEE MMM dd, yyyy hh:mma z", Locale.US)) When I try this it says java.time.format.DateTimeParseException: Text 'Wed Jul 16, 2016 4:38pm EDT' could not be parsed at index 17 So something is wrong with the hours? When I drop one of the 'h' it gets further ( altough it should just 0-pad my hours ), but then it doesn't like the

Java 8 Date equivalent to Joda's DateTimeFormatterBuilder with multiple parser formats?

眉间皱痕 提交于 2019-11-28 09:41:47
I currently have a Joda date parser that uses the DateTimeFormatterBuilder with half a dozen different date formats that I may receive. I'm migrating to Java 8's Date routines and don't see an equivalent. How can I do something like this using Java 8 Dates? DateTimeParser[] parsers = { DateTimeFormat.forPattern( "yyyy/MM/dd HH:mm:ss.SSSSSS" ).getParser() , DateTimeFormat.forPattern( "yyyy-MM-dd HH:mm:ss" ).getParser() , DateTimeFormat.forPattern( "ddMMMyyyy:HH:mm:ss.SSS Z" ).getParser() , DateTimeFormat.forPattern( "ddMMMyyyy:HH:mm:ss.SSS" ).getParser() , DateTimeFormat.forPattern( "ddMMMyyyy

DateTimeFormatter auto-corrects invalid (syntactically possible) calendar date

非 Y 不嫁゛ 提交于 2019-11-28 09:21:14
问题 Java DateTimeFormatter throws an exception for when you try a date that goes outside of a possible range, for example: DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/yyyy"); String dateString = "12/32/2015"; LocalDate ld = LocalDate.parse(dateString, dtf); will throw: Exception in thread "main" java.time.format.DateTimeParseException: Text '12/32/2015' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32 But when I enter an invalid calendar date that is

LocalDateTime to java.sql.Date in java 8?

流过昼夜 提交于 2019-11-28 09:11:17
How to convert LocalDateTime to java.sql.Date in java-8 ? My search on internet mostly give me Timestamp related code or LocalDate to java.sql.Date . I'm looking for LocalDateTime to java.sql.Date . There is no direct correlation between LocalDateTime and java.sql.Date , since former is-a timestamp, and latter is-a Date. There is, however, a relation between LocalDate and java.sql.Date , and conversion can be done like this: LocalDate date = //your local date java.sql.Date sqlDate = java.sql.Date.valueOf(date) Which for any given LocalDateTime gives you the following code: LocalDateTime

Is `Locale` needed for parsing date-time strings in Java?

故事扮演 提交于 2019-11-28 08:36:58
Under what conditions do I need a Locale for parsing date-time strings in Java? What does Locale have to do with time zone ? Sometimes I see Questions & Answers where the Locale was needed for the solution to a parsing problem. Yet in others there is no mention of Locale. Locale & Time Zone are unrelated Locale and time zone are separate, orthogonal issues with respect to date-time handling. Locale Language Human language, such as Arabic , French , Farsi . Text of the names of day-of-week, names of month, and ordinal indicators . For example… Is it Monday or Lundi ? Culture Commonly used

Can JAXB handle java.time objects?

℡╲_俬逩灬. 提交于 2019-11-28 08:10:40
I know JAXB (Java Architecture for XML Binding) can marshal/unmarshal java.util.Date objects as seen in this answer by Blaise Doughan . But what about the new java.time package objects in Java 8 , such as ZonedDateTime ? Has JAXB been updated to handle this newly built-in data type? bdoughan In Java SE 8, JAXB has not been updated yet to support the java.time types. Indeed, there is an issue related to this in the reference implementation. You need to create and use an XmlAdapter to handle those types. Use an approach similar to that done with Joda-Time as described in this posting, JAXB and

Java 8 - DateTimeFormatter and ISO_INSTANT issues with ZonedDateTime

半腔热情 提交于 2019-11-28 07:11:26
So I would expect this code to work under the new Java 8 date/time package since all it does is to convert a given ZonedDateTime to string and back using the same built-in DateTimeFormatter instance (ISO_INSTANT): ZonedDateTime now = ZonedDateTime.now(); System.out.println(ZonedDateTime.parse( now.format(DateTimeFormatter.ISO_INSTANT), DateTimeFormatter.ISO_INSTANT)); But apparently it doesn't: Exception in thread "main" java.time.format.DateTimeParseException: Text '2014-09-01T19:37:48.549Z' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=549,

What is the difference between year and year-of-era?

*爱你&永不变心* 提交于 2019-11-28 06:41:44
The DateTimeFormatter class documentation defines separate symbols u for year and y year-of-era: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns What is the difference between year and year-of-era? The answer lies in the documentation for IsoChronology era - There are two eras, 'Current Era' (CE) and 'Before Current Era' (BCE). year-of-era - The year-of-era is the same as the proleptic-year for the current CE era. For the BCE era before the ISO epoch the year increases from 1 upwards as time goes backwards. proleptic-year - The proleptic year is the