String、Date、LocalDateTime、Calendar与时间戳之间互相转化

String、Date、LocalDateTime、Calendar与时间戳之间互相转化参考String、Date、LocalDateTime、Calendar与时间戳之间互相转化

package com.zyp.test.time;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author syl
 * @description TODO
 * @since 2022/5/28
 */
public class DateTest {
    /**
     * 格式化对象实例
     */
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 格式化对象实例
     */
    private static final  DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * date与字符串的互转
     * @throws ParseException
     */
    @Test
    public void StringToDate() throws ParseException {
        //把date转成指定格式的字符串
        String nowString = SIMPLE_DATE_FORMAT.format(new Date());
        System.out.println("nowString:"+nowString);
        //把指定格式的字符串转成date
        Date nowDate = SIMPLE_DATE_FORMAT.parse(nowString);
        System.out.println("nowDate:"+nowDate);
        //122 ,结果不符合预期,不建议使用date获取年月日时分秒
        System.out.println("nowDate.getYear() = " + nowDate.getYear());
    }

    /**
     * date与calender互转
     */
    @Test
    public void dateToCalendar(){
        //获取Calendar实例,默认是当前时间
        Calendar calendar = Calendar.getInstance();
        //date转成calendar
        calendar.setTime(new Date());
        System.out.println(calendar.toString());
        //calendar转成date
        Date now = calendar.getTime();
        System.out.println(now);
    }

    /**
     * String与Calendar 此处借助中间类date
     */
    @Test
    public void stringToCalendar() throws ParseException {
        Calendar calendar=Calendar.getInstance();
        String dateString ="2018-12-08 12:00:08";
        //String--->Calendar
        calendar.setTime(SIMPLE_DATE_FORMAT.parse(dateString));
        //Calendar--->String
        String date1 = SIMPLE_DATE_FORMAT.format(calendar.getTime());
    }

    /**
     * String与LocalDateTime
     */
    @Test
    public void stringToLocalDateTime(){
        String dateString ="2018-12-08 12:00:08";
        // String转成LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.parse(dateString, DATE_TIME_FORMATTER);
        // LocalDateTime转成String
        String date = localDateTime.format(DATE_TIME_FORMATTER);
    }

    /**
     * date与LocalDateTime
     */
    @Test
    public void dateToLocalDateTime() throws ParseException {
        String dateString ="2018-12-08 12:00:08";
        Date date = SIMPLE_DATE_FORMAT.parse(dateString);
        //date转成LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        //LocalDateTime转成date
        Date date1 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(SIMPLE_DATE_FORMAT.format(date1));
    }

    /**
     * Calendar与LocalDateTime
     * @throws ParseException
     */
    @Test
    public void calendarToLocalDateTime() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        String dateString ="2018-12-08 12:00:08";
        Date date = SIMPLE_DATE_FORMAT.parse(dateString);
        calendar.setTime(date);
        //Calendar转成LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
        //LocalDateTime转成Calendar
        Calendar calendar1 = GregorianCalendar.from(localDateTime.atZone(ZoneId.systemDefault()));
        System.out.println("SIMPLE_DATE_FORMAT.format(calendar1.getTime()) = " + SIMPLE_DATE_FORMAT.format(calendar1.getTime()));
    }

    /**
     * 时间戳与Date、Calendar、LocalDateTime
     */
    @Test
    public void test(){
        long l = System.currentTimeMillis();
        System.out.println(l);
        Date date = new Date();
        date.setTime(l);
        date.getTime();
        System.out.println("SIMPLE_DATE_FORMAT.format(date) = " + SIMPLE_DATE_FORMAT.format(date));
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(l);
        calendar.getTimeInMillis();
        System.out.println("SIMPLE_DATE_FORMAT.format(calendar.getTime()) = " + SIMPLE_DATE_FORMAT.format(calendar.getTime()));
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(l), ZoneId.systemDefault());
        System.out.println("localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond() = " + localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond());
        System.out.println("localDateTime.format(DATE_TIME_FORMATTER) = " + localDateTime.format(DATE_TIME_FORMATTER));
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值