date
類表示特定的瞬間精確到毫秒,不過從api可以看出很多的方法已經廢棄了,這個類已經在過多使用了,不過還是需要了解一下的,為了後面的學習做鋪墊
date date=new date();
system.out.println(date.gettime());
******dateformat dateformat=new ******dateformat(); //預設的格式
string formatestring=dateformat.format(date); //格式化當前的日期
system.out.println(formatestring);
string model="yyyy-mm-dd-ff hh:mm:ss"; //指定格式化的模板
******dateformat dateformat2=new ******dateformat(model);
system.out.println(dateformat2.format(date));
string d = "2017-06-12 22:34:19"; //給出格式化後的日期
string pattern = "yyyy-mm-dd hh:mm:ss"; //按照上面的日期格式定義模板,這個一定要完全和上面的一樣,否則轉換不正確
******dateformat dateformat = new ******dateformat(pattern);
try catch (parseexception e)
scanner scanner=new scanner(system.in);
system.out.println("請輸入年-月-日");
string starttime=scanner.next();
system.out.println("請輸入結束時間(年-月-日)");
string endtime=scanner.next();
string moudle="yyyy-mm-dd"; //定義時間模板
//建立指定模板的解析
******dateformat dateformat=new ******dateformat(moudle);
date startdate=dateformat.parse(starttime);//解析開始時間
date enddate =dateformat.parse(endtime);//解析結束時間
long time=startdate.gettime()-enddate.gettime(); //返回兩個時間的差,毫秒
int day=(int)(time/1000/60/60/24); //轉化為天數,1秒等於1000毫秒,一分鐘等於60秒,一小時等於60分鐘,一天等於24小時
system.out.println(day);
calendar 類是乙個抽象類,它為特定瞬間與一組諸如 year、month、day_of_month、hour 等 日曆字段之間的轉換提供了一些方法,並為操作日曆字段(例如獲得下星期的日期)提供了一些方法。瞬間可用毫秒值來表示,它是距曆元(即格林威治標準時間 1970 年 1 月 1 日的 00:00:00.000,格里高利歷)的偏移量。
calendar calendar = calendar.getinstance(); // 建立物件
system.out.println(calendar.get(calendar.year));// 獲取年份
system.out.println(calendar.get(calendar.month) + 1);// 月,從0開始,即是輸出5表示6月
system.out.println(calendar.get(calendar.date));// 獲取乙個月中的第幾天
system.out.println(calendar.get(calendar.hour)); // 小時
system.out.println(calendar.get(calendar.minute)); // 分鐘
system.out.println(calendar.get(calendar.second)); // 秒
system.out.println(calendar.get(calendar.am_pm)); // 獲得是上午還是下午am=0,pm=1
system.out.println(calendar.get(calendar.day_of_month)); // 乙個月中的第幾天
system.out.println(calendar.get(calendar.day_of_week)); // 一周中的第幾天,星期日是第一天
system.out.println(calendar.get(calendar.day_of_year));// 一年中的第幾天
system.out.println(calendar.get(calendar.hour_of_day)); // 一天中的第幾小時
if (calendar.get(calendar.am_pm) == calendar.am)
if (calendar.get(calendar.month) + 1 == calendar.july)
calendar calendar=calendar.getinstance();
date date=calendar.gettime(); //獲得date物件
string pattern="yyyy-mm-dd hh:mm:ss";
******dateformat dateformat=new ******dateformat(pattern);
system.out.println(dateformat.format(date));
calendar calendar=calendar.getinstance();
calendar.add(calendar.date, -2);
system.out.println(calendar.get(calendar.date));
calendar calendar = calendar.getinstance();
int year = calendar.get(calendar.year);
int month = calendar.get(calendar.month) + 1; // 從0開始算,因此加1
int date = calendar.get(calendar.date);
int week = calendar.get(calendar.week_of_month) + 1; // 從週日開始算,因此加1
int hour = calendar.get(calendar.hour);
int minute = calendar.get(calendar.minute);
int seconds = calendar.get(calendar.second);
if (calendar.get(calendar.am_pm) == calendar.am) else
java日期格式化
日期格式化 這個比較簡單,不多說什麼了,一看便知。date date new date dateformat sdf new dateformat dateformat sdf1 new dateformat yyyy mm dd dateformat sdf2 new dateformat yyy...
java日期格式化
日期和時間模式 日期和時間格式由日期和時間模式 字串指定。在日期和時間模式字串中,未加引號的字母 a 到 z 和 a 到 z 被解釋為模式字母,用來表示日期或時間字串元素。文字可以使用單引號 引起來,以免進行解釋。表示單引號。所有其他字元均不解釋 只是在格式化時將它們簡單複製到輸出字串,或者在分析時...
java日期格式化
用法 dateformat dformat new dateformat yyyy mm dd hh mm ss.sss string currdate dformat.format new date system.out.println 現在的時間為 currdate1 補充 模式字母 摘自jdk...