java-time

How to create DateTimeformatter with optional seconds arguments

若如初见. 提交于 2019-12-01 05:56:58
I am trying to create a DateTimeformatter to validate following date times: String date1 = "2017-07-06T17:25:28"; String date2 = "2017-07-06T17:25:28.1"; String date3 = "2017-07-06T17:25:28.12"; String date4 = "2017-07-06T17:25:28.123"; String date5 = "2017-07-06T17:25:28.1234"; String date6 = "2017-07-06T17:25:28.12345"; String date7 = "2017-07-06T17:25:28.123456"; String date8 = "2017-07-06T17:25:28."; I have tried the following date time formatter to validate above dates: public static final String DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss"; DateTimeFormatter formatter1 = new

Convert date with ordinal numbers (11th, 22nd, etc.)

[亡魂溺海] 提交于 2019-12-01 05:51:53
问题 How to convert a date having the following format September 22nd 2015, 10:39:42 am to 09/22/2015 10:39:42 in Java 8? My current code: String value = "September 22nd 2015, 10:39:42 am"; String format = "dd/MM/yyyy HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(format); try { Date date = sdf.parse(value); System.out.println(date); System.out.println(sdf.format(date)); } catch (ParseException ex) { ex.printStackTrace(); } 回答1: The tricky part of the format is to handle ordinal numbers

Generate a random LocalDate with java.time

故事扮演 提交于 2019-12-01 05:19:30
I'm writing some code to populate a MySQL database with random data for testing purposes. I need to populate a DATE column with random dates from 1970-2015. Here's the relevant method: public Date dateGenerator() throws Exception { Random ry = new Random(); Random rm = new Random(); Random rd = new Random(); int year = 1969 + ry.nextInt(2015-1969+1); int month = 1 + rm.nextInt(12); int day = 1 + rm.nextInt(31); if (month==2 && day>28){ day = day - 3; } else { if((month%2==0 && month != 8 ) && day==31 ){ day = day -1; } } } My purpose is to create three random integers (for day, month, year)

TZupdater failing with tzdata2016g release

牧云@^-^@ 提交于 2019-12-01 03:38:06
TZUpdater 2.1.0 is failing with tzdata2016g release. For Java 8 it fails with "Source directory does not contain file: VERSION" error, while it completes with "JRE updated to version : tzdataunknown" comment for Java 7. The reason of this seems to be recent change of IANA tzdata distribution: Unsetting VERSION field of Makefile. There is a bug reported regarding the issue: https://bugs.openjdk.java.net/browse/JDK-8166928 . DST date(30th October 2016) is getting closer and we at least need a workaround for this. Is it, somehow, possible? Workaround: Update Makefile of http://www.iana.org/time

Java 8 Offset Date Parsing

无人久伴 提交于 2019-12-01 03:30:58
I need to parse a String in the following format 2015-01-15-05:00 to LocalDate(or smth else) in UTC. The problem is that the following code: System.out.println(LocalDate.parse("2015-01-15-05:00", DateTimeFormatter.ISO_OFFSET_DATE)); outputs 2015-01-15 ignoring the offset. The desired output is 2015-01-16 Thanks in advance! The simplest answer is to use OffsetDateTime to represent the data, but you need to default the time: DateTimeFormatter fmt = new DateTimeFormatterBuilder() .append(DateTimeFormatter.ISO_OFFSET_DATE) .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) .toFormatter(); OffsetDateTime

Parsing date and AM/PM using java 8 DateTime API

不想你离开。 提交于 2019-12-01 03:19:59
I'm trying to parse a date time string using java 8 DateTime API. The string to parse contains date, month, year and AM/PM marker such as 17/02/2015 PM . I use the following pattern: dd/MM/yyyy aa and I expect the parsed LocalDateTime time part to be set at 12:00:00 or 00:00:00 depending on the AM/PM marker. Using the former java.text.SimpleDateFormat API, the parsing works as expected using this pattern. But when i use java 8 DateTime API and run the following code: LocalDateTime.parse("17/02/2015 PM", DateTimeFormatter.ofPattern("dd/MM/yyyy aa")); I got the following exception: Exception in

java.time : DateTimeParseException for date “20150901023302166” [duplicate]

霸气de小男生 提交于 2019-12-01 03:16:45
This question already has an answer here: Is java.time failing to parse fraction-of-second? 3 answers LocalDateTime.parse("20150901023302166", DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) gives the error: java.time.format.DateTimeParseException: Text '20150901023302166' could not be parsed at index 0 A workaround is to build the formatter yourself using DateTimeFormatterBuilder and fixed width for each field. This code produces the correct result. public static void main(String[] args) { DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4)

Generate a random LocalDate with java.time

▼魔方 西西 提交于 2019-12-01 02:13:45
问题 I'm writing some code to populate a MySQL database with random data for testing purposes. I need to populate a DATE column with random dates from 1970-2015. Here's the relevant method: public Date dateGenerator() throws Exception { Random ry = new Random(); Random rm = new Random(); Random rd = new Random(); int year = 1969 + ry.nextInt(2015-1969+1); int month = 1 + rm.nextInt(12); int day = 1 + rm.nextInt(31); if (month==2 && day>28){ day = day - 3; } else { if((month%2==0 && month != 8 ) &&

java.time : DateTimeParseException for date “20150901023302166” [duplicate]

天涯浪子 提交于 2019-12-01 00:22:51
问题 This question already has answers here : Is java.time failing to parse fraction-of-second? (3 answers) Closed 4 years ago . LocalDateTime.parse("20150901023302166", DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) gives the error: java.time.format.DateTimeParseException: Text '20150901023302166' could not be parsed at index 0 回答1: A workaround is to build the formatter yourself using DateTimeFormatterBuilder and fixed width for each field. This code produces the correct result. public static

Determine future timezone transitions

杀马特。学长 韩版系。学妹 提交于 2019-11-30 23:35:19
问题 I need to predict when the next at least 2 timezone transitions will be for a particular timezone. Java 8 offers the new java.time API, specifically java.time.zone . ZoneRules.getTransitions() looks like exactly what I need however it doesn't list anything beyond the year 2010 for "Australia/Sydney". What is the most reliable way to determine the next 2 timezone transition's date/time/offset? 回答1: The method ZoneRules.getTransitions() doesn't list all transitions until infinity into the