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
  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)\""));
  }
}
Esempio n. 4
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. 5
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. 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
 @Test
 public void testLocalTime() {
   System.out.println(LocalTime.now());
   System.out.println(LocalTime.now(ZoneId.of("Asia/Tokyo")));
   System.out.println(LocalTime.ofSecondOfDay(10000));
 }