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
相關文章
相關標籤/搜索