Therefore, we need an option to convert different date/time related objects between each other. Of course we do not always have objects of the type we need. LocalDate fromCustomPattern = LocalDate.parse("", DateTimeFormatter.ofPattern("dd.MM.yyyy")) Conversion LocalDate fromIsoWeekDate = LocalDate.parse("2014-W14-2", DateTimeFormatter.ISO_WEEK_DATE) LocalDate fromIsoDate = LocalDate.parse("") String germanDateTime = dateTime.format(formatter) using short german date/time formatting (01.04.14 10:45)ĭateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) String frenchDate = dateTime.format(DateTimeFormatter.ofPattern("d. String asCustomPattern = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")) String asIsoDateTime = dateTime.format(DateTimeFormatter.ISO_DATE_TIME) String asIsoWeekDate = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE) String asBasicIsoDate = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE)
#JAVA LOCALDATE TIME MINUS MINUTES ISO#
format as basic ISO date format (20140220) LocalDateTime dateTime = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45) In Java 8 this can be accomplished by using the format() and parse() methods: Long twoHoursInSeconds = Duration.ofHours(2).getSeconds() Formatting and Parsingįormatting and parsing is another big topic when working with dates and times. Long absoluteResult = between.abs().toMinutes() negative because firstInstant is after secondInstant (-172920) Instant secondInstant = Instant.ofEpochSecond(1294708260) // 01:11ĭuration between = Duration.between(firstInstant, secondInstant) LocalDate eleventhOfMarch = us(twoMonthsAndFiveDays) add two months and five days to, result is
LocalDate sixthOfJanuary = LocalDate.of(2014, 1, 6) Period twoMonthsAndFiveDays = Period.ofMonths(2).plusDays(5) Period period = Period.between(firstDate, secondDate) īoolean isNegative = period.isNegative() // false LocalDate secondDate = LocalDate.of(2015, 3, 7) // LocalDate firstDate = LocalDate.of(2010, 5, 17) // A Period uses date based values (years, months, days) while a Duration uses seconds or nanoseconds to define an amount of time. Duration is most suitable when working with Instants and machine time. Periods and Durations can contain negative values if the end point occurs before the starting point. Like the names suggest they represent a quantity or amount of time. Period and Duration are two other important classes. Instant nowPlusTenSeconds = now.plusSeconds(10) Periods and Durations Long toUnixTimestamp = now.getEpochSecond() toString() returns ISO 8601 format, e.g. Instant fromEpochMilli = Instant.ofEpochMilli(1262347200000l) Instant fromUnixTimestamp = Instant.ofEpochSecond(1262347200) They follow ISO 8601 the standard for representing date and time. Instant values can be negative if they occured before the epoch. An Instant counts the time beginning from the first second of Janu( 00:00:00) also called the EPOCH. For this we can use the Instant class which represents timestamps. However, often we need to work with time viewed from a machine perspective. OffsetDateTime minusTwo = plusFive.withOffsetSameInstant(ZoneOffset.ofHours(-2)) TimestampsĬlasses like LocalDate and ZonedDateTime provide a human view on time. OffsetDateTime plusFive = OffsetDateTime.of(date, offset) ZoneOffset offset = ZoneOffset.of("+05:00") LocalDateTime date = LocalDateTime.of(2013, Month.JULY, 20, 3, 30) Set allZoneIds = ZoneId.getAvailableZoneIds() Int offsetInSeconds = losAngelesDateTime.getOffset().getTotalSeconds() // -28800
ZonedDateTime losAngelesDateTime = berlinDateTime.withZoneSameInstant(losAngeles) ZonedDateTime berlinDateTime = ZonedDateTime.of(dateTime, berlin) LocalDateTime dateTime = LocalDateTime.of(2014, 02, 20, 12, 0) ZoneId berlin = ZoneId.of("Europe/Berlin") ZoneId losAngeles = ZoneId.of("America/Los_Angeles") For example, we can use the Year class if we need information about a specific year: Some information can be obtained without providing a specific date. Int secondOfDay = time.toSecondOfDay() // 55800 LocalDateTime startOfDay = date.atStartOfDay() // 00:00 Int dayOfMonth = date.getDayOfMonth() // 15 String dayOfWeekName = dayOfWeek.name() // SATURDAY Int dayOfWeekIntValue = dayOfWeek.getValue() // 6 Int lengthOfYear = date.lengthOfYear() // 365īoolean isLeapYear = date.isLeapYear() // falseĭayOfWeek dayOfWeek = date.getDayOfWeek() Int dayOfYear = date.getDayOfYear() // 46 Month firstMonthOfQuarter = february.firstMonthOfQuarter() // JANUARY Int maxLength = february.maxLength() // 29 Int minLength = february.minLength() // 28 Int februaryIntValue = february.getValue() // 2 Month february = date.getMonth() // FEBRUARY
LocalDate date = LocalDate.of(2014, 2, 15) // īoolean isBefore = LocalDate.now().isBefore(date) // false