赞
踩
- public class TestMain {
-
- public static void main(String[] args) {
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- // 1、普通的时间转换
- String string = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();
- System.out.println(string);
- System.out.println("-------------------------------");
- // 2、日历类的时间操作
- Calendar calendar = Calendar.getInstance();
- System.out.println(calendar.get(Calendar.YEAR));
- System.out.println(calendar.get(Calendar.MONTH) + 1);
- System.out.println(calendar.get(Calendar.DATE));
- System.out.println("当前时间: " + dateFormat.format(calendar.getTime()));
- // 获取当前时间前3秒
- calendar.add(Calendar.SECOND, -3);// 3秒之前的时间
- System.out.println("当前时间3秒之前: " + dateFormat.format(calendar.getTime()));
- // 获取当前时间前3分钟
- calendar.add(Calendar.MINUTE, -3);// 3分钟之前的时间
- System.out.println("当前时间3分钟之前: " + dateFormat.format(calendar.getTime()));
-
- }
- }
输出:
- 2022-02-11
- -------------------------------
- 2022
- 2
- 11
- 当前时间: 2022-02-11 11:05:28
- 当前时间3秒之前: 2022-02-11 11:05:25
- 当前时间3分钟之前: 2022-02-11 11:02:25
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。