java-time

Get first and last day of month using threeten, LocalDate

北战南征 提交于 2019-12-28 08:06:42
问题 I have a LocalDate which needs to get the first and last day of the month. How do I do that? eg. 13/2/2014 I need to get 1/2/2014 and 28/2/2014 in LocalDate formats. Using threeten LocalDate class. 回答1: Just use withDayOfMonth , and lengthOfMonth() : LocalDate initial = LocalDate.of(2014, 2, 13); LocalDate start = initial.withDayOfMonth(1); LocalDate end = initial.withDayOfMonth(initial.lengthOfMonth()); 回答2: The API was designed to support a solution that matches closely to business

Unable to obtain OffsetDateTime from TemporalAccessor

淺唱寂寞╮ 提交于 2019-12-28 04:23:28
问题 When I do this String datum = "20130419233512"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneId.of("Europe/Berlin")); OffsetDateTime datetime = OffsetDateTime.parse(datum, formatter); I get the following exception: java.time.format.DateTimeParseException: Text '20130419233512' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1366407312},ISO,Europe/Berlin resolved to 2013-04-19T23:35:12 of type java.time

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

馋奶兔 提交于 2019-12-28 03:52:26
问题 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? 回答1: 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

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

大憨熊 提交于 2019-12-28 03:52:09
问题 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? 回答1: 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

Get original pattern String given a JDK 8 DateTimeFormatter?

旧街凉风 提交于 2019-12-28 03:04:28
问题 Related to my question here - how do I get the original pattern String given a DateTimeFormatter ? 回答1: It's been asked on the mailing list and the answer is that it is not possible because the original pattern is not retained. The same thread suggests using a DateTimeFormatterBuilder which does have the information. 回答2: It may not be a direct answer to your question but it may help. If you know the parameters of how it the formatter was constructed you can call the static method:

JSR-310 - parsing seconds fraction with variable length

非 Y 不嫁゛ 提交于 2019-12-28 02:51:25
问题 Is there a way how to create JSR-310 formatter that is able to parse both following date/times with variable length of seconds fraction? 2015-05-07 13:20:22.276052 or 2015-05-07 13:20:22.276 Example code: DateTimeFormatter formatter = new java.time.format.DateTimeFormatterBuilder() .append( java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") ) .appendOptional( java.time.format.DateTimeFormatter.ofPattern(".SSSSSS") ) .toFormatter(); formatter.parse("2015-05-07 13:20:22.276052"

How to check if a date Object equals yesterday?

ぐ巨炮叔叔 提交于 2019-12-27 11:49:08
问题 Right now I am using this code Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE) - 1, 12, 0, 0); //Sets Calendar to "yeserday, 12am" if(sdf.format(getDateFromLine(line)).equals(sdf.format(cal.getTime()))) //getDateFromLine() returns a Date Object that is always at 12pm {...CODE There's got to be a smoother way to check if the date returned by getdateFromLine() is

How to check if a date Object equals yesterday?

孤街浪徒 提交于 2019-12-27 11:46:07
问题 Right now I am using this code Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE) - 1, 12, 0, 0); //Sets Calendar to "yeserday, 12am" if(sdf.format(getDateFromLine(line)).equals(sdf.format(cal.getTime()))) //getDateFromLine() returns a Date Object that is always at 12pm {...CODE There's got to be a smoother way to check if the date returned by getdateFromLine() is

Format Instant to String

泄露秘密 提交于 2019-12-27 11:02:50
问题 I'm trying to format an Instant to a String using the new java 8 time-api and a pattern: Instant instant = ...; String out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(instant); Using the code above I get an Exception which complains an unsupported field: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra at java.time.Instant.getLong(Instant.java:608) at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) ... 回答1: Time

Comparing Dates in Java 7 using Calendar

谁都会走 提交于 2019-12-24 19:04:21
问题 I am comparing the start Date of a treatement to its End to check if it lasts more than 6 months. If the period doesn't include the month of February all is good but if I compare the 1st of January to the 30th of June, it throws my Exception. To compare both periods I add 6 months to the start Date and compare the result to the end Date like so: Date start = new Date(2017,1,1); Date end = new Date(2017,6,30); Calendar startOfTreatment = new Calendar.getInstance(); startOfTreatment.setTime