【持续更新】Java 时间相关

直接上代码:

 1 import java.util.*;
 2 import java.text.SimpleDateFormat;
 3 
 4 public class HelloWorld {
 5     public static void main(String[] args) {
 6         //可以方便地修改日期格式
 7         Date now = new Date();
 8         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
 9         String dateTime = dateFormat.format(now);
10         System.out.println(dateTime);
11 
12         //可以对每个时间域单独修改
13         Calendar c = Calendar.getInstance();
14         int year = c.get(Calendar.YEAR);
15         int month = c.get(Calendar.MONTH); //1月为0,2月为1,如此类推
16         int date = c.get(Calendar.DATE);
17         int hour = c.get(Calendar.HOUR_OF_DAY);
18         int minute = c.get(Calendar.MINUTE);
19         int second = c.get(Calendar.SECOND);
20         System.out.println(year + "/" + (month + 1) + "/" + date + " " + hour + ":" + minute + ":" + second);
21     }
22 }

 

 输出:

2019/03/25 17:19:34
2019/3/25 17:19:34

 

posted @ 2019-03-25 17:21  lipohong  阅读(353)  评论(0编辑  收藏  举报