zoukankan      html  css  js  c++  java
  • JAVA 各种时间类型转换

    final Date date = new Date();
        final Timestamp timestamp = new Timestamp(date.getTime());
        final Calendar calendar = Calendar.getInstance();
        
        final Instant instant = Instant.now();
        final LocalDateTime localDateTime = LocalDateTime.now();
        final ZonedDateTime zonedDateTime = ZonedDateTime.now();
    
    initialize

    转Date

    //Timestamp转Date
        Date timestampDate = new Date(timestamp.getTime());
        //Calendar转Date
        Date calendarDate = calendar.getTime();
        //Instant转Date
        Date instantDate = Date.from(instant);
        //LocalDateTime转Date
        Date localDateTimeDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        //ZonedDateTime转Date
        Date zonedDateTimeDate = Date.from(zonedDateTime.toInstant());

    转Instant

    //Date转Instant
        Instant dateInstant = date.toInstant();
        //Timestamp转Instant
        Instant timestampInstant = timestamp.toInstant();
        //Calendar转Instant
        Instant calendarInstant = calendar.toInstant();
        //LocalDateTime转Instant
        Instant localDateTimeInstant = localDateTime.toInstant(ZoneOffset.of(ZoneId.systemDefault().getId()));
        //ZonedDateTime转Instant
        Instant zonedDateTimeInstant = zonedDateTime.toInstant();

    转LocalDateTime

    //Date转LocalDateTime
        LocalDateTime dateLocalDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        //Timestamp转LocalDateTime
        LocalDateTime timestampLocalDateTime = timestamp.toLocalDateTime();
        //Calendar转LocalDateTime
        LocalDateTime calendarLocalDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());
        //Instant转LocalDateTime
        LocalDateTime instantLocalDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        //ZonedDateTime转LocalDateTime
        LocalDateTime zonedDateTimeLocalDateTime = zonedDateTime.toLocalDateTime();

    转ZonedDateTime

    //Date转ZonedDateTime
        ZonedDateTime dateZonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        //Timestamp转ZonedDateTime
        ZonedDateTime timestampZonedDateTime = ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
        //Calendar转ZonedDateTime
        ZonedDateTime calendarZonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
        //Instant转ZonedDateTime
        ZonedDateTime instantZonedDateTime = instant.atZone(ZoneId.systemDefault());
        //LocalDateTime转ZonedDateTime
        ZonedDateTime localDateTimeZonedDateTime = localDateTime.atZone(ZoneId.systemDefault());

    转Timestamp

    //Date转Timestamp
        Timestamp dateTimestamp = new Timestamp(date.getTime());
        //Calendar转Timestamp
        Timestamp calendarTimestamp = new Timestamp(calendar.getTimeInMillis());
        //Instant转Timestamp
        Timestamp instantTimestamp = Timestamp.from(instant);
        //LocalDateTime转Timestamp
        Timestamp localDateTimeTimestamp = Timestamp.valueOf(localDateTime);
        //ZonedDateTime转Timestamp
        Timestamp zonedDateTimeTimestamp = Timestamp.from(zonedDateTime.toInstant());

    转Calendar

    //Date转Calendar
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        //Timestamp转Calendar
        Calendar timestampCalendar = Calendar.getInstance();
        timestampCalendar.setTimeInMillis(timestamp.getTime());
        //Instant转Calendar
        Calendar instantCalendar = GregorianCalendar.from(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));
        //LocalDateTime转Calendar
        Calendar localDateTimeCalendar = GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
        //ZonedDateTime转Calendar
        Calendar zonedDateTimeInstantCalendar = GregorianCalendar.from(zonedDateTime);
  • 相关阅读:
    linux查看java jdk安装路径和设置环境变量
    linq where in 排序
    Console程序后台运行
    winform中文本框,软键盘跟随
    winform 应用log4net做日志记录到mysql
    c# 单实例运行
    Sql Server数据库监听 c#代码
    winform程序开机自动启动
    c# 连接mysql配置config,不用装net connector
    winform 不规则窗体无锯齿demo
  • 原文地址:https://www.cnblogs.com/chuyi-/p/10039650.html
Copyright © 2011-2022 走看看