Esempio n. 1
0
  public static void main(String[] args) {
    DecimalFormat decimalFormat = new DecimalFormat("00");
    System.out.println(decimalFormat.format(7));

    //        f3();

    Instant timestamp = Instant.ofEpochMilli(System.currentTimeMillis());
    System.out.println(timestamp);

    Instant current = Clock.system(ZoneId.of("Asia/Shanghai")).instant();
    System.out.println(ZoneId.of("Asia/Shanghai").getRules());
    System.out.println(current);
    //        System.out.println(System.currentTimeMillis());
    //        System.out.println(Instant.now().getEpochSecond());
  }
Esempio n. 2
0
  @Test
  public void testBasic() {
    System.out.println(Year.now());
    System.out.println(Year.of(2015));
    System.out.println(Year.isLeap(2016));

    Locale locale = Locale.getDefault();
    System.out.println(Month.DECEMBER.getDisplayName(TextStyle.FULL, locale));
    System.out.println(Month.DECEMBER.getDisplayName(TextStyle.SHORT, locale));
    System.out.println(Month.DECEMBER.getDisplayName(TextStyle.NARROW, locale));
    System.out.println(Month.DECEMBER.getDisplayName(TextStyle.FULL_STANDALONE, locale));
    System.out.println(Month.of(8).ordinal());
    System.out.println(Month.AUGUST.minus(2));

    System.out.println(YearMonth.now());
    System.out.println(MonthDay.now());

    System.out.println(DayOfWeek.FRIDAY.plus(2));
    System.out.println(DayOfWeek.of(1));
    System.out.println(
        DayOfWeek.from(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())));

    System.out.println(Period.between(Year.now().atDay(1), LocalDate.now()));
    System.out.println(ChronoUnit.DAYS.between(Year.now().atDay(1), LocalDate.now()));
  }
Esempio n. 3
0
  public static void main(String[] args) {
    ZonedDateTime apollo11launch =
        ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0, ZoneId.of("America/New_York"));

    String formatted = DateTimeFormatter.ISO_DATE_TIME.format(apollo11launch);
    // 1969-07-16T09:32:00-05:00[America/New_York]
    System.out.println(formatted);

    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
    formatted = formatter.format(apollo11launch);
    // July 16, 1969 9:32:00 AM EDT
    System.out.println(formatted);
    formatted = formatter.withLocale(Locale.FRENCH).format(apollo11launch);
    // 16 juillet 1969 09:32:00 EDT
    System.out.println(formatted);

    formatter = DateTimeFormatter.ofPattern("E yyyy-MM-dd HH:mm");
    formatted = formatter.format(apollo11launch);
    System.out.println(formatted);

    LocalDate churchsBirthday = LocalDate.parse("1903-06-14");
    System.out.println("churchsBirthday: " + churchsBirthday);
    apollo11launch =
        ZonedDateTime.parse(
            "1969-07-16 03:32:00-0400", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssxx"));
    System.out.println("apollo11launch: " + apollo11launch);
  }
public class ApacheCombinedFormatLoggerTest {
  LogRecordingHandler logRecords = new LogRecordingHandler();
  Instant currentTime = LocalDateTime.of(2012, 6, 27, 12, 4, 0).toInstant(ZoneOffset.of("-05:00"));
  ApacheCombinedFormatLogger apacheCommonLogger =
      new ApacheCombinedFormatLogger(
          anonymousLogger(logRecords), Clock.fixed(currentTime, ZoneId.of("GMT+01:00")), Locale.US);

  Request request = new Request().protocol("HTTP/1.1").remoteIp("192.168.0.1");
  Response response = new Response();

  @Test
  public void logsRequestsServedInApacheCombinedLogFormat() throws Exception {
    request
        .method(GET)
        .uri("/products?keyword=dogs")
        .addHeader(HeaderNames.REFERER, "http://lama/wool")
        .addHeader(HeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)");

    apacheCommonLogger.handle(request, response);
    response.status(OK).body("a response with a size of 28").done();

    response.await();
    logRecords.assertEntries(
        contains(
            "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"http://lama/wool\" \"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)\""));
  }

  @Test
  public void logsEmptyStringWhenNoUserAgentInRequest() throws Exception {
    request
        .method(GET)
        .uri("/products?keyword=dogs")
        .addHeader(HeaderNames.REFERER, "http://lama/wool");

    apacheCommonLogger.handle(request, response);
    response.status(OK).body("a response with a size of 28").done();

    response.await();
    logRecords.assertEntries(
        contains(
            "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"http://lama/wool\" \"\""));
  }

  @Test
  public void logsEmptyStringWhenNoRefererInRequest() throws Exception {
    request
        .method(GET)
        .uri("/products?keyword=dogs")
        .addHeader(HeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)");

    apacheCommonLogger.handle(request, response);
    response.status(OK).body("a response with a size of 28").done();

    response.await();
    logRecords.assertEntries(
        contains(
            "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"\" \"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)\""));
  }
}
  public static ZonedDateTime getNextSchedule() {
    ZonedDateTime next_schedule;

    // Determine type of schedule
    if (ConfigHandler.backupInterval > 0) // Interval
    {
      next_schedule =
          ZonedDateTime.ofInstant(
              Instant.ofEpochMilli(
                  System.currentTimeMillis() + (ConfigHandler.backupInterval * 60 * 1000)),
              ZoneId.systemDefault());
    } else // Schedule
    {
      if (ConfigHandler.backupSchedule.length == 0) {
        return null;
      }

      LocalTime now = LocalTime.now();
      LocalTime next_time = null;
      LocalDate day = LocalDate.now();
      TreeSet<LocalTime> times = new TreeSet<>();

      for (String s : ConfigHandler.backupSchedule) {
        times.add(LocalTime.parse(s, DateTimeFormatter.ofPattern("H:mm")));
      }

      for (LocalTime t : times) // try to find next scheduled time for today
      {
        if (t.compareTo(now) == 1) {
          next_time = t;
          break;
        }
      }

      if (next_time
          == null) // if we couldn't find one for today take the first schedule time for tomorrow
      {
        day = day.plusDays(1);
        next_time = times.first();
      }

      next_schedule = ZonedDateTime.of(day, next_time, ZoneId.systemDefault());
    }

    return next_schedule;
  }
Esempio n. 6
0
  @Test
  public void shouldPlayWithClocks() {
    Clock c1 = Clock.systemDefaultZone();
    print(c1.getZone() + "|" + c1.instant());

    StringBuilder builder = new StringBuilder();
    for (String zoneId : ZoneId.getAvailableZoneIds()) builder.append(zoneId).append(",");
    print(builder);

    ZoneId z1 = ZoneId.of("Australia/Tasmania");
    Clock c2 = c1.withZone(z1);
    print(c2.instant().atZone(z1));

    LocalDate d1 = LocalDate.now(c1);
    LocalDate d2 = LocalDate.now(c2);
    print(d1 + "|" + d2);
  }
Esempio n. 7
0
  public static void main(String[] args) {
    LocalDateTime localDateTime = LocalDateTime.of(2016, 3, 27, 1, 20);
    localDateTime = localDateTime.plusHours(1);
    System.out.println(localDateTime);

    ZoneId zoneId = ZoneId.of("Europe/Warsaw");
    ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
    System.out.println(zonedDateTime);
  }
Esempio n. 8
0
  public static void main(String[] args) {
    ZonedDateTime apollo11launch =
        ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0, ZoneId.of("America/New_York"));
    // 1969-07-16T09:32-04:00[America/New_York]
    System.out.println("apollo11launch: " + apollo11launch);

    Instant instant = apollo11launch.toInstant();
    System.out.println("instant: " + instant);

    ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("UTC"));
    System.out.println("zonedDateTime: " + zonedDateTime);

    ZonedDateTime skipped =
        ZonedDateTime.of(
            LocalDate.of(2013, 3, 31), LocalTime.of(2, 30), ZoneId.of("Europe/Berlin"));
    // Constructs March 31 3:30
    System.out.println("skipped: " + skipped);

    ZonedDateTime ambiguous =
        ZonedDateTime.of(
            LocalDate.of(2013, 10, 27), // End of daylight savings time
            LocalTime.of(2, 30),
            ZoneId.of("Europe/Berlin"));
    // 2013-10-27T02:30+02:00[Europe/Berlin]
    ZonedDateTime anHourLater = ambiguous.plusHours(1);
    // 2013-10-27T02:30+01:00[Europe/Berlin]
    System.out.println("ambiguous: " + ambiguous);
    System.out.println("anHourLater: " + anHourLater);

    ZonedDateTime meeting =
        ZonedDateTime.of(
            LocalDate.of(2013, 10, 31), LocalTime.of(14, 30), ZoneId.of("America/Los_Angeles"));
    System.out.println("meeting: " + meeting);
    ZonedDateTime nextMeeting = meeting.plus(Duration.ofDays(7));
    // Caution! Won’t work with daylight savings time
    System.out.println("nextMeeting: " + nextMeeting);
    nextMeeting = meeting.plus(Period.ofDays(7)); // OK
    System.out.println("nextMeeting: " + nextMeeting);
  }
Esempio n. 9
0
  @Test
  public void testLocalDateTime() {
    System.out.println(LocalDateTime.now());
    System.out.println(LocalDateTime.of(1994, Month.MAY, 15, 11, 30));
    System.out.println(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
    System.out.println(LocalDateTime.ofEpochSecond(1450749600, 0, ZoneOffset.ofHours(8)));

    System.out.printf("6 months from now: %s%n", LocalDateTime.now().plusMonths(6));
    System.out.printf("6 days ago: %s%n", LocalDateTime.now().minusDays(6));

    System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now()));
    System.out.println(DateTimeFormatter.ofPattern("MM-dd HH:mm").format(LocalDateTime.now()));
  }
  public List<String> follows(String userName) {

    List<HashMap<String, String>> messages =
        new ArrayList<HashMap<String, String>>(
            restTemplate
                .getForObject(baseURL + userName + "/follows.json", Resources.class)
                .getContent());

    LocalDateTime ldtNow =
        (new Date()).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

    List<String> following = new ArrayList<>();

    for (HashMap<String, String> mapMessage : messages) {
      following.add(mapMessage.get("followsUser"));
    }

    return following;
  }
 @Override
 public LocalDateTime convert(Date source) {
   return source == null
       ? null
       : LocalDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
 }
 @Override
 public Date convert(LocalDateTime source) {
   return source == null ? null : Date.from(source.atZone(ZoneId.systemDefault()).toInstant());
 }
Esempio n. 13
0
 public static void main(String[] args) {
   String joined = String.join("/", "usr", "local", "bin"); // "usr/local/bin"
   System.out.println(joined);
   String ids = String.join(", ", ZoneId.getAvailableZoneIds());
   System.out.println(ids);
 }
Esempio n. 14
0
 @Test
 public void testLocalTime() {
   System.out.println(LocalTime.now());
   System.out.println(LocalTime.now(ZoneId.of("Asia/Tokyo")));
   System.out.println(LocalTime.ofSecondOfDay(10000));
 }