Java时间转换

package org.jh.modules.spt.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class cs {
    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        //获取当前日期
        System.out.println("1  " + date);
        //获取当前时间戳
        System.out.println("2  " + date.getTime());
        //获取当前时间年月日
        SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("3  " + a.format(date));
        //获取当前年时间时分秒
        SimpleDateFormat b = new SimpleDateFormat("hh:mm:ss");
        System.out.println("4  " + b.format(date));
        //获取当前年时间年月日时分秒
        SimpleDateFormat c = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("5  " + c.format(date));
        //时间戳转换成年月日时分秒
        //获取当前时间的时间戳
        Date date1 = new Date(System.currentTimeMillis());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("6  " + dateFormat.format(date1));
        //年月日换成时间戳
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date parse = d.parse("2022-01-06 09:20:19");
        System.out.println("7  " + parse.getTime());
    }
}

-------------------------结果-------------------------
1  Thu Jan 06 09:22:02 CST 2022
2  1641432122241
3  2022-01-06
4  09:22:02
5  2022-01-06 09:22:02
6  2022-01-06 09:22:02
7  1641432019000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值