java--Date時間


Date: 表示特定的瞬間,精確到毫秒,通過方法設定自己所表示的時間,可以表示任意的時間
System.currentTimeMillis() :返回的當前系統時間, 1970-1-1 至今的毫秒數

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");  // 使用指定的模式進行時間對象的構建

//格式化
Date date = new Date();
String s = sdf.format(date);
System.out.println(s);


//解析
Date d = sdf.parse("2019年06月21日18:22:02");
System.out.println(d.toLocalString());  //2019-06-21-18


Calendar: 日歷,提供了一些操作年月日的方法
//static Calender getInstance()
Calender c = Calender.getInstance();

//void set(int field,int value):把指定的字段修改為指定的值
c.set(Calendar.YEAR, 2019);
c.set(Calendar.MONTH, 5);
c.set(Calendar.DAY_OF_MONTH, 20);


//void add(int field, int value):指定的字段增加value
c.set(Calendar.DAT_OF_MONTH, 1)


//int get(int field)  // 返回給定日歷字段的值
//public static final int YEAR 1

//int year = c.get(1);  //2019
int year = c.get(Calendar.YEAR);  //2019
int month = c.get(Calendar.MONTH) + 1; // 6
int day = c.get(Calendar.DAY_OF_MONTH); //21


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM