Java-各种时间格式转换

本文详细介绍了使用Java进行日期和时间的操作方法,包括如何将UTC时间转换为北京时间、如何将字符串转换为Date类型以及如何将Date类型转换为字符串。此外,还介绍了如何在原时间基础上加一天的具体实现。
@Test
    public void text() throws ParseException {

        //将“1996-08-08T16:00:00.000Z”UTC时间转换成北京时间
        String cBirth = "1996-08-08T16:00:00.000Z";
        cBirth = cBirth.replace("Z"," UTC");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        //此时输出为:Fri Aug 09 00:00:00 CST 1996
        System.out.println(sdf.parse(cBirth));


        //将"yyyy-MM-dd"格式时间字符串转化成Data类型
        String data ="2019-08-23";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");//字符串格式
        //输出结果为:Fri Aug 23 00:00:00 CST 2019
        System.out.println(sdf2.parse(data));


        //Date时间格式转换成字符串输出
        Timestamp a = new Timestamp(System.currentTimeMillis());//数据库DateTime类型对应
        Date d = new Date();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//输出格式
        System.out.println(sdf1.format(d));
        System.out.println(sdf1.format(a));


        //在原时间上加上一天
        Date now = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        c.add(Calendar.DAY_OF_MONTH, 1);
        Date tomorrow = c.getTime();

        System.out.println(now);
        System.out.println(tomorrow);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值