Java Code Examples for java.time.LocalDateTime.minusNanos()

The following are Jave code examples for showing how to use minusNanos() of the java.time.LocalDateTime class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: GitHub   File: Issue1493.java   Source Code and License Vote up 5 votes
public void test_for_issue() throws Exception {

        TestBean test = new TestBean();
        String stime2 = "2017-09-22 15:08:56";

        LocalDateTime time1 = LocalDateTime.now();
        time1 = time1.minusNanos(10L);
        System.out.println(time1.getNano());
        LocalDateTime time2 = LocalDateTime.parse(stime2, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA));
        test.setTime1(time1);
        test.setTime2(time2);
        String t1 = JSON.toJSONString(time1, SerializerFeature.WriteDateUseDateFormat);

        String json = JSON.toJSONString(test, SerializerFeature.WriteDateUseDateFormat);
        Assert.assertEquals("{\"time1\":"+t1+",\"time2\":\""+stime2+"\"}",json);


        //String default_format = JSON.DEFFAULT_LOCAL_DATE_TIME_FORMAT;
        //JSON.DEFFAULT_LOCAL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
        //String stime1 = DateTimeFormatter.ofPattern(JSON.DEFFAULT_LOCAL_DATE_TIME_FORMAT, Locale.CHINA).format(time1);

        json = JSON.toJSONString(test, SerializerFeature.WriteDateUseDateFormat);
        Assert.assertEquals("{\"time1\":"+ JSON.toJSONString(time1, SerializerFeature.WriteDateUseDateFormat) +",\"time2\":\""+stime2+"\"}",json);


        String pattern = "yyyy-MM-dd HH:mm:ss";
        String stime1 = DateTimeFormatter.ofPattern(pattern, Locale.CHINA).format(time1);

        json = JSON.toJSONStringWithDateFormat(test, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat);
        Assert.assertEquals("{\"time1\":\""+stime1+"\",\"time2\":\""+stime2+"\"}",json);

        //JSON.DEFFAULT_LOCAL_DATE_TIME_FORMAT = default_format;
    }
 
Example 2
Project: jdk8u-jdk   File: TCKLocalDateTime.java   Source Code and License Vote up 5 votes
@Test
public void test_minusNanos_halfABillion() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate().minusDays(1);

    int hour = 0;
    int min = 0;
    int sec = 0;
    int nanos = 0;

    for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) {
        t = t.minusNanos(500000000);
        nanos -= 500000000;

        if (nanos < 0) {
            sec--;
            nanos += 1000000000;

            if (sec == -1) {
                min--;
                sec += 60;

                if (min == -1) {
                    hour--;
                    min += 60;

                    if (hour == -1) {
                        hour += 24;
                    }
                }
            }
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
        assertEquals(t.getSecond(), sec);
        assertEquals(t.getNano(), nanos);
    }
}
 
Example 3
Project: jdk8u-jdk   File: TCKLocalDateTime.java   Source Code and License Vote up 5 votes
@Test(dataProvider="minusNanos_fromZero")
public void test_minusNanos_fromZero(long nanoseconds, LocalDate date, int hour, int min, int sec, int nanos) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.minusNanos(nanoseconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
    assertEquals(nanos, t.getNano());
}
 
Example 4
Project: openjdk-jdk10   File: TCKLocalDateTime.java   Source Code and License Vote up 5 votes
@Test
public void test_minusNanos_halfABillion() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate().minusDays(1);

    int hour = 0;
    int min = 0;
    int sec = 0;
    int nanos = 0;

    for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) {
        t = t.minusNanos(500000000);
        nanos -= 500000000;

        if (nanos < 0) {
            sec--;
            nanos += 1000000000;

            if (sec == -1) {
                min--;
                sec += 60;

                if (min == -1) {
                    hour--;
                    min += 60;

                    if (hour == -1) {
                        hour += 24;
                    }
                }
            }
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
        assertEquals(t.getSecond(), sec);
        assertEquals(t.getNano(), nanos);
    }
}
 
Example 5
Project: openjdk-jdk10   File: TCKLocalDateTime.java   Source Code and License Vote up 5 votes
@Test(dataProvider="minusNanos_fromZero")
public void test_minusNanos_fromZero(long nanoseconds, LocalDate date, int hour, int min, int sec, int nanos) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.minusNanos(nanoseconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
    assertEquals(nanos, t.getNano());
}
 
Example 6
Project: cakes   File: LocalDateTimeDemo.java   Source Code and License Vote up 4 votes
/**
 * 时间计算,减法操作
 */
@Test
public void testClac(){
    LocalDateTime ldt = LocalDateTime.now();

    System.out.println(ldt);

    // 减: 天
    LocalDateTime days = ldt.minusDays(1);
    System.out.println("day: " + days);

    // 减: 小时
    LocalDateTime hours = ldt.minusHours(1);
    System.out.println("hours: " + hours);

    // 减: 分钟
    LocalDateTime minutes = ldt.minusMinutes(1);
    System.out.println("minutes: " + minutes);

    // 减: 月
    LocalDateTime months = ldt.minusMonths(1);
    System.out.println("months: " + months);

    // 减: 纳秒
    LocalDateTime nanos = ldt.minusNanos(1);
    System.out.println("nanos: " + nanos);

    // 减: 秒
    LocalDateTime seconds = ldt.minusSeconds(1);
    System.out.println("seconds: " + seconds);

    // 减: 周
    LocalDateTime weeks = ldt.minusWeeks(1);
    System.out.println("weeks: " + weeks);

    // 减: 年
    LocalDateTime years = ldt.minusYears(1);
    System.out.println("years: " + years);

    System.out.println(ldt);
}