1. 程式人生 > >Java中時間

Java中時間

class api文檔 ast 天數 時間差 minimal bsp settime throw

/**

*觀看API文檔學習:Date中很多方法失效;Calendar頂替。

*

**/
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) throws ParseException {
/**
* 計算 "2012/3/17" "2012-5-28"間隔天數

* 1.毫秒值獲取相減
* 2.獲取毫秒值,字符串--》日期對象-->毫秒值
*/
getDays();
}
public static void getSeconds(){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df .parse("2017-06-25");
//Date date1=new Date();
Date date1 = df.parse("2017-06-24");
Calendar cal = Calendar.getInstance();
Calendar cal1=Calendar.getInstance();
cal.setTime(date);
cal1.setTime(date1);
long timestamp = cal.getTimeInMillis();
long timestamp1=cal1.getTimeInMillis();
System.out.println(timestamp1-timestamp);
}
public static void getDays() throws ParseException{
String str_date1="2012/3/17";
String str_date2="2012-5-28";
//日期格式字符串解析成日期對象
//1.自定義yyyy/MM/dd風格對象
DateFormat dateFormat1=new SimpleDateFormat("yyyy/MM/dd");
//2.定義一個默認風格
DateFormat dateFormat2=DateFormat.getDateInstance(DateFormat.MEDIUM);
//查錯誤驗證 如果系統為英文默認是yy/mm/dd
Date date=new Date();
String str_date=dateFormat2.format(date);
System.out.println(str_date);

//3.對日期格式字符串進行解析
Date date1=dateFormat1.parse(str_date1);
Date date2=dateFormat2.parse(str_date2);
//4.通過日期對象獲取毫秒值
long time1=date1.getTime();
long time2=date2.getTime();
//5.相減
long time=Math.abs(time2-time1);
//6.毫秒轉day
int day=(int) (time/1000/60/60/24);
System.out.println(day);

}
public static void cal(){
//演示日歷
Calendar ca=Calendar.getInstance();
// System.out.println(ca);
//日歷:將時間相關的東西以鍵值對的方式全部封裝在一個map集合
/*
*
* java.util.GregorianCalendar[time=1497856585181,areFieldsSet=true,
* areAllFieldsSet=true,lenient=true,
* zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
* offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],
* firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=5,WEEK_OF_YEAR=25,
* WEEK_OF_MONTH=4,DAY_OF_MONTH=19,DAY_OF_YEAR=170,DAY_OF_WEEK=2,
* DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=3,
* HOUR_OF_DAY=15,MINUTE=16,SECOND=25,MILLISECOND=181,ZONE_OFFSET=28800000,DST_OFFSET=0]
*
*/
//對日歷對象其中的日期進行定義
//ca.set(Calendar.YEAR, 2015);
//ca.set(2012, 2, 2);
//日期偏移
//ca.add(Calendar.YEAR, -4);
//日期偏移比較狠
//ca.add(Calendar.MONTH, 7);
//showDate(ca);
//任意一年2月有多少天?3月1日的前一天。
//int year=2014;
//ca.set(year, 2,1);
//ca.add(Calendar.DAY_OF_MONTH, -1);
// showDate(ca);
//獲取昨天的現在時刻。
ca.add(Calendar.DAY_OF_MONTH, -1);
showDate(ca);
}
public static void showDate(Calendar ca){
int year=ca.get(Calendar.YEAR);
int month=ca.get(Calendar.MONTH)+1;
int day=ca.get(Calendar.DAY_OF_MONTH);
int week=ca.get(Calendar.DAY_OF_WEEK);
System.out.println(year+"年"+month+"月"+day+"日"+week);

}
public static void date() {
Date date = new Date();
System.out.println(date);
// 格式化--日期轉文本 解析--文本轉日期
// DateFormat 提供了很多類方法,以獲得基於默認或給定語言環境和多種格式化風格的默認日期/時間 Formatter。
// 格式化風格包括 FULL、LONG、MEDIUM 和 SHORT。方法描述中提供了使用這些風格的更多細節和示例。
// 通過DateFormat類中的靜態工廠方法獲取實例

DateFormat df = DateFormat.getDateInstance();

// 如果格式一般我們還可以加入style
df = DateFormat.getDateInstance(DateFormat.SHORT);
// FULL:2017年6月19日 星期一
// Long:2017年6月19日
// MEDIUM:2017-6-19
// SHORT:17-6-19
// 轉成自定義格式 XXXX/XX/XX 只能自定義對象 .使用子類//去SimpleDateFormat類總找日期和時間模式

df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");

// 日期+時間
DateFormat df1 = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL);
String dateformat1 = df1.format(date);
System.out.println(dateformat1);

// 使用DateFormat的format方法對日期對象進行格式化

String dateformat = df.format(date);
System.out.println(dateformat);
}
public static void date2() {
// 毫秒值--》日期對象 構造函數 setTime方法
long time = System.currentTimeMillis();
System.out.println(time);
Date date = new Date(time);
System.out.println(date);
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
String df_date = df.format(date);
System.out.println(df_date);
// 日期對象--》毫秒值 計算時間差 (時間戳)
long time1 = date.getTime();
System.out.println(time1);
}
public static void date1() throws ParseException {
Date date = new Date();
System.out.println(date);// 後臺輸出系統時間 但是格式看不懂
// 格式轉化 xxxx-yy-mm
String str = "2016-09-07";
DateFormat df = DateFormat.getDateInstance();

Date date1 = df.parse(str);
System.out.println(date1);
}
}

Java中時間