java处理时间和日期api

package _9time;

import java.text.*;
import java.time.*;
import java.time.format.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

public class Main {
    static String timestampToString(long epochMilli, Locale lo, String zoneId) {
        Instant ins = Instant.ofEpochMilli(epochMilli);
        DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
        return f.withLocale(lo).format(ZonedDateTime.ofInstant(ins, ZoneId.of(zoneId)));
    }
    public static void main(String[] args) {
        //Date
        int n = 123400;
        // 123400
        System.out.println(n);
        // 1e208
        System.out.println(Integer.toHexString(n));
        // $123,400.00
        System.out.println(NumberFormat.getCurrencyInstance(Locale.US).format(n));

        // 获取当前时间:
        Date date = new Date();
        System.out.println(date.getYear() + 1900); // 必须加上1900
        System.out.println(date.getMonth() + 1); // 0~11,必须加上1
        System.out.println(date.getDate()); // 1~31,不能加1
        // 转换为String:
        System.out.println(date.toString());
        // 转换为GMT时区:
        System.out.println(date.toGMTString());
        // 转换为本地时区:
        System.out.println(date.toLocaleString());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));

        SimpleDateFormat sdf1 = new SimpleDateFormat("E MMM dd, yyyy");
        System.out.println(sdf1.format(date));

        //Calendar
        // 获取当前时间:
        Calendar c = Calendar.getInstance();
        int y = c.get(Calendar.YEAR);
        int m = 1 + c.get(Calendar.MONTH);
        int d = c.get(Calendar.DAY_OF_MONTH);
        int w = c.get(Calendar.DAY_OF_WEEK);
        int hh = c.get(Calendar.HOUR_OF_DAY);
        int mm = c.get(Calendar.MINUTE);
        int ss = c.get(Calendar.SECOND);
        int ms = c.get(Calendar.MILLISECOND);
        System.out.println(y + "-" + m + "-" + d + " " + w + " " + hh + ":" + mm + ":" + ss + "." + ms);

        // 清除所有:
        c.clear();
        // 设置2019年:
        c.set(Calendar.YEAR, 2019);
        // 设置9月:注意8表示9月:
        c.set(Calendar.MONTH, 8);
        // 设置2日:
        c.set(Calendar.DATE, 2);
        // 设置时间:
        c.set(Calendar.HOUR_OF_DAY, 21);
        c.set(Calendar.MINUTE, 22);
        c.set(Calendar.SECOND, 23);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime()));
        // 2019-09-02 21:22:23

        //TimeZone
        TimeZone tzDefault = TimeZone.getDefault(); // 当前时区
        TimeZone tzGMT9 = TimeZone.getTimeZone("GMT+09:00"); // GMT+9:00时区
        TimeZone tzNY = TimeZone.getTimeZone("America/New_York"); // 纽约时区
        System.out.println(tzDefault.getID()); // Asia/Shanghai
        System.out.println(tzGMT9.getID()); // GMT+09:00
        System.out.println(tzNY.getID()); // America/New_York
        System.out.println(TimeZone.getAvailableIDs().toString());


        //北京时间转为纽约时间
        // 清除所有:
        c.clear();
        // 设置为北京时区:
        c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        // 设置年月日时分秒:
        c.set(2019, 10 /* 11月 */, 20, 8, 15, 0);
        // 显示时间:
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf2.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        System.out.println(sdf2.format(c.getTime()));
        // 2019-11-19 19:15:00


        //时间加减
        // 清除所有:
        c.clear();
        // 设置年月日时分秒:
        c.set(2019, 10 /* 11月 */, 20, 8, 15, 0);
        // 加5天并减去2小时:
        c.add(Calendar.DAY_OF_MONTH, 5);
        c.add(Calendar.HOUR_OF_DAY, -2);
        // 显示时间:
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d3 = c.getTime();
        System.out.println(sdf3.format(d3));
        // 2019-11-25 6:15:00

        //LocalDateTime
        LocalDate d4 = LocalDate.now(); // 当前日期
        LocalTime t4 = LocalTime.now(); // 当前时间
        LocalDateTime dt4 = LocalDateTime.now(); // 当前日期和时间
        System.out.println(d4); // 严格按照ISO 8601格式打印
        System.out.println(t4); // 严格按照ISO 8601格式打印
        System.out.println(dt4); // 严格按照ISO 8601格式打印

        // 指定日期和时间:
        LocalDate d5 = LocalDate.of(2019, 11, 30); // 2019-11-30, 注意11=11月
        LocalTime t5 = LocalTime.of(15, 16, 17); // 15:16:17
        LocalDateTime dt5 = LocalDateTime.of(2019, 11, 30, 15, 16, 17);
        LocalDateTime dt55 = LocalDateTime.of(d5, t5);
        System.out.println(d5);
        System.out.println(t5);
        System.out.println(dt5);
        System.out.println(dt55);


        LocalDateTime dt6 = LocalDateTime.parse("2019-11-19T15:16:17");
        LocalDate d6 = LocalDate.parse("2019-11-19");
        LocalTime t6 = LocalTime.parse("15:16:17");
        System.out.println(dt6);
        System.out.println(d6);
        System.out.println(t6);

        //DateTimeFormatter
        // 自定义格式化:
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        System.out.println(dtf.format(LocalDateTime.now()));

        // 用自定义格式解析:
        LocalDateTime dt2 = LocalDateTime.parse("2019/11/30 15:16:17", dtf);
        System.out.println(dt2);

        //时间加减
        LocalDateTime dt = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
        System.out.println(dt);
        // 加5天减3小时:
        LocalDateTime dt7 = dt.plusDays(5).minusHours(3);
        System.out.println(dt7); // 2019-10-31T17:30:59
        // 减1月:
        LocalDateTime dt8 = dt7.minusMonths(1);
        System.out.println(dt8); // 2019-09-30T17:30:59


        //时间日期调整
        LocalDateTime dt9 = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
        System.out.println(dt9);
        // 日期变为31日:
        LocalDateTime dt10 = dt9.withDayOfMonth(31);
        System.out.println(dt10); // 2019-10-31T20:30:59
        // 月份变为9:
        LocalDateTime dt11 = dt10.withMonth(9);
        System.out.println(dt11); // 2019-09-30T20:30:59

        //with()
        // 本月第一天0:00时刻:
        LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();
        System.out.println("本月第一天0:00时刻:"+firstDay);

        // 本月最后1天:
        LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("本月最后1天:"+lastDay);

        // 下月第1天:
        LocalDate nextMonthFirstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());
        System.out.println("下月第1天:"+nextMonthFirstDay);

        // 本月第1个周一:
        LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        System.out.println("本月第1个周一:"+firstWeekday);


        //判断时间先后
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime target = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
        System.out.println(now.isBefore(target));
        System.out.println(LocalDate.now().isBefore(LocalDate.of(2019, 11, 19)));
        System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00")));

        //Duration和Period 时间间隔,日期间隔
        LocalDateTime start = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
        LocalDateTime end = LocalDateTime.of(2020, 1, 9, 19, 25, 30);
        Duration d2 = Duration.between(start, end);
        Duration d21 = Duration.ofHours(10); // 10 hours
        Duration d22 = Duration.parse("P1DT2H3M"); // 1 day, 2 hours, 3 minutes
        System.out.println(d2); // PT1235H10M30S
        System.out.println(d21); // PT1235H10M30S
        System.out.println(d22); // PT1235H10M30S

        Period p = LocalDate.of(2019, 11, 19).until(LocalDate.of(2020, 1, 9));
        System.out.println(p); // P1M21D

        /**ZonedDateTime*/
        ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区
        ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间
        System.out.println(zbj);
        System.out.println(zny);


        // 以中国时区获取当前时间:
        ZonedDateTime zbj1 = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        // 转换为纽约时间:
        ZonedDateTime zny1 = zbj1.withZoneSameInstant(ZoneId.of("America/New_York"));
        System.out.println(zbj1);
        System.out.println(zny1);

        ZonedDateTime zdt = ZonedDateTime.now();
        LocalDateTime ldt = zdt.toLocalDateTime();
        System.out.println(ldt);

        /**DateTimeFormatter*/
        ZonedDateTime zdt1 = ZonedDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm ZZZZ");
        System.out.println(formatter.format(zdt1));

        DateTimeFormatter zhFormatter = DateTimeFormatter.ofPattern("yyyy MMM dd EE HH:mm", Locale.CHINA);
        System.out.println(zhFormatter.format(zdt1));

        DateTimeFormatter usFormatter = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US);
        System.out.println(usFormatter.format(zdt1));

        LocalDateTime ldt1 = LocalDateTime.now();
        System.out.println(DateTimeFormatter.ISO_DATE.format(ldt1));
        System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(ldt1));


        //Instant 当前时间戳

        Instant instant = Instant.now();
        System.out.println(instant.getEpochSecond());
        System.out.println(instant.toEpochMilli());


        // 以指定时间戳创建Instant:
        Instant ins = Instant.ofEpochSecond(1568568760);
        ZonedDateTime zdt2 = ins.atZone(ZoneId.systemDefault());
        System.out.println(zdt2); // 2019-09-16T01:32:40+08:00[Asia/Shanghai]

        /**旧API转新API*/
        // Date -> Instant:
        Instant ins1 = new Date().toInstant();

        // Calendar -> Instant -> ZonedDateTime:
        Calendar calendar = Calendar.getInstance();
        Instant ins2 = Calendar.getInstance().toInstant();
        ZonedDateTime zdt3 = ins2.atZone(calendar.getTimeZone().toZoneId());
        System.out.println(zdt3);

        /**新API转旧API*/
        // ZonedDateTime -> long:
        ZonedDateTime zdt4 = ZonedDateTime.now();
        long ts = zdt4.toEpochSecond() * 1000;

        // long -> Date:
        Date date1 = new Date(ts);

        // long -> Calendar:
        Calendar calendar1 = Calendar.getInstance();
        calendar1.clear();
        calendar1.setTimeZone(TimeZone.getTimeZone(zdt.getZone().getId()));
        calendar1.setTimeInMillis(zdt4.toEpochSecond() * 1000);


        //数据库时间
        long ts1 = 1574208900000L;
        System.out.println(timestampToString(ts1, Locale.CHINA, "Asia/Shanghai"));
        System.out.println(timestampToString(ts1, Locale.US, "America/New_York"));
    }
}

 

java处理时间和日期apijava处理时间和日期api qubeijun 发布了18 篇原创文章 · 获赞 5 · 访问量 1万+ 私信 关注
上一篇:JAVA-日期处理-LocalDate-时区


下一篇:Java日期类(2)-JDK8之后的日期API