Date:表示特定的瞬間,精確到毫秒。java
Date():根據當前的默認毫秒值建立日期對象
Date(long date):根據給定的毫秒值建立日期對象
複製代碼
getTime()
setTime(long time)
複製代碼
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
Date d1 = new Date();
System.out.println("d1:" + d1);
Date d2 = new Date(System.currentTimeMillis());
System.out.println("d2:" + d2);
}
}
//運行結果
d1:Mon Jun 10 22:02:00 CST 2019
d2:Mon Jun 10 22:02:00 CST 2019
複製代碼
Date → String(格式化)學習
//(這個是具體子類SimpleDateFormat的父類DateForamt中的一個方法)
Public final String format(Date date) 複製代碼
String→ Date(解析) parse ()spa
public Date parse(String source) 複製代碼
DateFormat:code
能夠進行日期和字符串的格式化和解析,可是因爲是抽象類,因此使用具體子類SimpleDateFormat。orm
/* * 其中 yyyy 是完整的公元年,MM 是月份,dd 是日期,HH:mm:ss 是時、分、秒。 * 注意:有的格式大寫,有的格式小寫 * 例如 MM 是月份,mm 是分;HH 是 24 小時制,而 hh 是 12小時制。 */
package cn.bwh_02_DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatDemo {
public static void main(String[] args) {
Date d = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Current Date:" + ft.format(d));
}
}
//運行結果
Current Date:星期一 2019.06.10 at 10:23:43 下午 CST
複製代碼
案例:計算某一時間到如今的天數cdn
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/* * 計算某一時間到如今的天數 */
public class DateFormatDemo2 {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入過去的一個時間點(以「 - 」分隔):");
String line = sc.nextLine();
//把字符串轉換爲一個日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(line);
//經過該日期獲取一個毫秒值
long oldTime = d.getTime();
//獲取當前時間毫秒值
long nowTime = System.currentTimeMillis();
long time = nowTime - oldTime;
long day = time/1000/60/60/24;
System.out.println("這個時間過去:" + day + "天");
}
}
//運行結果
請輸入過去的一個時間點(以「 - 」分隔):
2019-5-20
這個時間過去:21天
複製代碼
Calendar類是一個抽象類,經常使用語操做日曆字段如 YEAR,MONTH,DAY_OF_MONTH,HOUR等對象
//返回給定字段的值
public int get(int field) //根據給定的日曆字段和對應的時間,來對當前的日曆進行操做。 //(amount能夠是負的) public void add(int field,int amount) //設置當前日曆的年月日 public final void set(int year,int month,int date) 複製代碼
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar rightNow = Canlendar.getInstance();//子類對象
//獲取年月日
int year = rightNow.get(Calendar.YEAR);
int month = rightNow.get(Calendar.MONTH);
int date = rightNow.get(Calendar.DATE);
System.out.println(year + "年" + (month -1) + "月" + date + "日");
}
}
複製代碼
案例:輸出指定年份的日曆blog
import java.util.Calendar;
import java.util.Scanner;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
Scanner sc = new Scanner(System.in);
System.out.println("請輸入年份:");
int year = sc.nextInt();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);
while (c.get(Calendar.YEAR) == year) {
int weekday = c.get(Calendar.DAY_OF_WEEK);
int monthday = c.get(Calendar.DAY_OF_MONTH);
if (monthday == 1) {
System.out.println("\n日\t一\t二\t三\t四\t五\t六\t第" + (c.get(Calendar.MONTH) + 1) + "月");
System.out.println("---------------------------------------------------");
for (int i = 0; i < weekday - 1; i++)
System.out.print("\t");
}
System.out.print(monthday + "\t");
if (weekday == 7)
System.out.println();
c.add(Calendar.DAY_OF_MONTH, 1);
}
sc.close();
}
}
複製代碼
若是內容中有什麼不足,或者錯誤的地方,歡迎你們給我留言提出意見, 蟹蟹你們 !^_^字符串
若是能幫到你的話,那就來關注我吧!(系列文章均會在公衆號第一時間更新)get
在這裏的咱們素不相識,卻都在爲了本身的夢而努力 ❤
一個堅持推送原創Java技術的公衆號:理想二旬不止