Package java.time

Examples of java.time.LocalDateTime


                    Message[] msgs = source.search(st);
                    if (msgs != null && msgs.length > 0) {
                        moveMessages(msgs, source, getFolder(rule.getDestFolder(), Folder.READ_WRITE));
                    }
                } else if (rule.getOlderThan() > 0) {
                    LocalDateTime day = LocalDateTime.now().minusDays(rule.getOlderThan());
                    SentDateTerm term = new SentDateTerm(ComparisonTerm.LE,
                            Date.from(day.toLocalDate().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
                    Message[] msgs = source.search(term);
                    if (msgs != null && msgs.length > 0) {
                        deleteMessages(msgs, source);
                    }
                }
View Full Code Here


*/
public class LocalDateTime1 {

    public static void main(String[] args) {

        LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);

        DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
        System.out.println(dayOfWeek);      // WEDNESDAY

        Month month = sylvester.getMonth();
        System.out.println(month);          // DECEMBER

        long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
        System.out.println(minuteOfDay);    // 1439

        Instant instant = sylvester
                .atZone(ZoneId.systemDefault())
                .toInstant();

        Date legacyDate = Date.from(instant);
        System.out.println(legacyDate);     // Wed Dec 31 23:59:59 CET 2014


        DateTimeFormatter formatter =
                DateTimeFormatter
                        .ofPattern("MMM dd, yyyy - HH:mm");

        LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter);
        String string = parsed.format(formatter);
        System.out.println(string);     // Nov 03, 2014 - 07:13
    }
View Full Code Here

   * Date/time classes provide factory methods which follow naming conventions. of() is used to construct date/time from
   * their constituent field, from a year, up to a resolution of nanoseconds.
   */
  @Test
  public void testCreateDateTimeFromConstituentFields() {
    final LocalDateTime localDateTime = LocalDateTime.of(2014, Month.AUGUST, 30, 21, 40, 20);
    // toString() methods return ISO-8601 format representation by default, which is consistent with parsing (see below)
    assertThat(localDateTime.toString(), is("2014-08-30T21:40:20"));
  }
View Full Code Here

   */
  @Test
  public void testParseDateTime() {
    // Parse a local (no time zone) date/time string
    // Uses java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME.
    final LocalDateTime localdateTime = LocalDateTime.parse("2014-08-30T21:40:20");
    assertThat(localdateTime, is(LocalDateTime.of(2014, Month.AUGUST, 30, 21, 40, 20)));

    // Parse a date/time string with a time zone, in ISO-8601 format - this one is in UTC, as per the 'Z' zone indicator
    // Uses java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.
    final ZonedDateTime zonedDateTime = ZonedDateTime.parse("2014-08-30T21:40:20Z");
View Full Code Here

  public void testGetDateTimeComponentValues() {
    final Month month = Month.AUGUST;
    int dayOfMonth = 31;
    int seconds = 59;

    final LocalDateTime localDateTime = LocalDateTime.of(2014, month, dayOfMonth, 18, 46, seconds);

    assertThat(localDateTime.getMonth(), is(month));
    assertThat(localDateTime.getDayOfMonth(), is(dayOfMonth));
    assertThat(localDateTime.getSecond(), is(seconds));
  }
View Full Code Here

   * Date/time classes are immutable. Therefore methods to alter instances of them are named with() rather than being
   * setters, and they return new instances.
   */
  @Test
  public void testAlterDateTimeComponentValues() {
    final LocalDateTime localDateTime = LocalDateTime.of(2014, Month.AUGUST, 30, 19, 02, 58);

    // Test altering component fields with literal values using withXXX()
    int pastYear = 2013;
    int pastDayOfMonth = 1;
    final LocalDateTime pastDateTime = localDateTime.withYear(pastYear).withDayOfMonth(pastDayOfMonth);
    // Construct separate LocalDate and Time from supplied component values, and time component of existing DateTime
    LocalDate expectedDate = LocalDate.of(pastYear, localDateTime.getMonth(), pastDayOfMonth);
    final LocalTime expectedTime = localDateTime.toLocalTime();
    // A LocalDateTime can be constructed from separate LocalDate and LocalTime
    assertThat(pastDateTime, is(LocalDateTime.of(expectedDate, expectedTime)));

    // Test altering component fields using delta method plusXXX()
    LocalDateTime futureDateTime = localDateTime.plusWeeks(3);
    expectedDate = LocalDate.of(localDateTime.getYear(), Month.SEPTEMBER, 20);
    assertThat(futureDateTime, is(LocalDateTime.of(expectedDate, expectedTime)));
    // Test altering component fields using generic delta method plus(value, Unit)
    futureDateTime = futureDateTime.plus(1, ChronoUnit.WEEKS);
    expectedDate = LocalDate.of(localDateTime.getYear(), Month.SEPTEMBER, 27);
    assertThat(futureDateTime, is(LocalDateTime.of(expectedDate, expectedTime)));
  }
View Full Code Here

   * The API supports truncating a Date, Time and DateTime to different precisions. Truncation results in the fields
   * with precisions smaller than the specified unit being set to zero.
   */
  @Test
  public void testTruncationTo() {
    final LocalDateTime localDateTime = LocalDateTime.of(2014, Month.SEPTEMBER, 1, 15, 56, 52, 1);
    // Note - To set seconds and nanoseconds to zero, you truncate the minutes.
    LocalDateTime truncatedDateTime = localDateTime.truncatedTo(ChronoUnit.MINUTES);
    assertThat(truncatedDateTime, is(LocalDateTime.parse("2014-09-01T15:56:00")));
  }
View Full Code Here

  public void testDurationToAlterTime() {
    // A duration of 3 seconds and 5 nanoseconds
    Duration duration = Duration.ofSeconds(3, 5);

    // You can modify the values of a DateTime using a Duration
    final LocalDateTime localDateTime = LocalDateTime.of(2014, Month.SEPTEMBER, 1, 17, 29, 40);
    final LocalDateTime futureDateTime = localDateTime.plus(duration);
    assertThat(futureDateTime, is(LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.of(17, 29, 43, 5))));
    // You can also use a Duration to calculate the difference between two times
    assertThat(Duration.between(localDateTime, futureDateTime), is(duration));
  }
View Full Code Here

        assertThat(result).isEqualTo(LocalDateTime.of(1970, 1, 1, 1, 0));
    }

    @Test
    public void operationIsSymetric() {
        final LocalDateTime originalDate = LocalDateTime.of(2014, 1, 1, 17, 10, 23);
        final Java8LocalDateTimeConverter converter = new Java8LocalDateTimeConverter();
        final Object serialized = converter.toDbValue(originalDate);
        final Object unserialized = converter.fromDbValue(serialized);

        assertThat(unserialized).isEqualTo(originalDate);
View Full Code Here

            if (LHUtils.hashFromPledge(pledge).equals(myPledgeHash))
                msg += " (yours)";
            status.setText(msg);
            email.setText(pledge.getPledgeDetails().getContactAddress());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            LocalDateTime time = LocalDateTime.ofEpochSecond(pledge.getTimestamp(), 0, ZoneOffset.UTC);
            date.setText(time.format(formatter));
            memoSnippet.setText(pledge.getPledgeDetails().getMemo());
        }
View Full Code Here

TOP

Related Classes of java.time.LocalDateTime

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.