Calendar.getInstance().getTime()获取时间

new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime());

1. 使用Calendar.getInstance()不给参数获取当前的时间

public class Test {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String time = sdf.format(calendar.getTime());
        System.out.println(time);
    }
}

运行结果:

2022-04-15 16:42:18:327

2. 获取指定时间

public class Test {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,8);//获取时间的小时设置为8点
        calendar.set(Calendar.MINUTE,22);//获取时间的分钟设置为22分
        calendar.set(Calendar.SECOND,32);//获取时间的秒设置为32秒
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String time = sdf.format(calendar.getTime());
        System.out.println(time);
    }
}

运行结果

2022-04-15 08:22:32:320

原文链接:https://blog.csdn.net/m0_47010003/article/details/124198407
posted @ 2022-10-19 13:45  独苏  阅读(414)  评论(0编辑  收藏  举报