?? dateformatutilstest.java~11~
字號:
package com.lianjiping.democ;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DateFormatUtilsTest {
public static void main(String[] args) {
long currentTimeMillis = DateFormatUtils.getCurrentTimeMillis();
System.out.println("系統(tǒng)的當(dāng)前時間(毫秒): " + currentTimeMillis);
Date currentDate = DateFormatUtils.getCurrentDate();
System.out.println("系統(tǒng)的當(dāng)前時間: " + currentDate);
String currentFormatDate = DateFormatUtils.getCurrentFormatDate();
System.out.println("系統(tǒng)的當(dāng)前日期(默認(rèn)格式): " + currentFormatDate);
String currentFormatDateTime = DateFormatUtils.getCurrentFormatDateTime();
System.out.println("系統(tǒng)的當(dāng)前日期時間(默認(rèn)格式): " + currentFormatDateTime);
String currentCustomFomatDateTime12 = DateFormatUtils.
getCurrentCustomFormatDateTime("yyyy-MM-dd HH:mm ");
System.out.println("系統(tǒng)的當(dāng)前日期時間(指定格式返回): " + currentCustomFomatDateTime12);
String formatDate = DateFormatUtils.formatDate(currentDate, "yyMM");
System.out.println("輸入日期(指定格式返回): " + formatDate);
String formatDate1 = DateFormatUtils.formatDate(currentDate,
"yyyyMMdd ", new Locale("zh", "CN", ""));
System.out.println("輸入日期(指定格式返回): " + formatDate1);
Date strToDate = DateFormatUtils.parseStrToDate(currentFormatDateTime);
System.out.println("按照默認(rèn)格式轉(zhuǎn)換為Date: " + strToDate);
Date strToDateTime = DateFormatUtils.parseStrToDateTime(
currentFormatDateTime);
System.out.println("按照默認(rèn)格式轉(zhuǎn)換為Date: " + strToDateTime);
Calendar cal = DateFormatUtils.parseStrToCalendar(currentFormatDateTime);
System.out.println("按照默認(rèn)格式轉(zhuǎn)換為Calender: " +
cal.get(Calendar.YEAR) + "年" +
cal.get(Calendar.MONTH) + "月" +
cal.get(Calendar.DAY_OF_MONTH) + "日" +
cal.get(Calendar.HOUR_OF_DAY) + ":" +
cal.get(Calendar.MINUTE) + ":" +
cal.get(Calendar.SECOND));
String dateTimeStr = DateFormatUtils.parseDateStrToDateTimeStr(
currentFormatDate);
System.out.println(" Test 11: DateStr = " + currentFormatDate +
" to DateTimeStr = " + dateTimeStr);
Date strToFormatDate = DateFormatUtils.parseStrToCustomPatternDate(
currentFormatDateTime, DateFormatUtils.DATE_TIME_FORMAT);
Date strToFormatDateTime = DateFormatUtils.parseStrToCustomPatternDate(
currentFormatDateTime, "");
System.out.println(" Test 12: parse String to \n " +
" \t\tcustom pattern Date: " + strToFormatDate +
" \n " +
" \t\tcustom pattern Date without pattern: " +
strToFormatDateTime);
System.out.println(" Test 13: " +
DateFormatUtils.formatDate(strToFormatDateTime,
DateFormatUtils.DATE_TIME_FORMAT));
String convertPattern = DateFormatUtils.convertDatePattern(
currentFormatDateTime, DateFormatUtils.DATE_TIME_FORMAT,
DateFormatUtils.DATE_TIME_FORMAT_14);
System.out.println(" Test 14: pattern from: " +
currentFormatDateTime +
" \n\t\tpattern to: " + convertPattern);
Date addDays = DateFormatUtils.addDays(strToDate, 35);
System.out.println(" Test 15: now -- " + strToDate +
" ,after add 30 days, \n\t the time is " +
addDays);
Date minusDays = DateFormatUtils.minusDays(addDays, 35);
System.out.println(" Test 16: now -- " + addDays +
" ,after minus 30 days, \n\t the time is " +
minusDays);
String addYear = DateFormatUtils.addDate(currentFormatDateTime,
DateFormatUtils.
DATE_TIME_FORMAT, " year ", 2);
String addMonth = DateFormatUtils.addDate(currentFormatDateTime,
DateFormatUtils.
DATE_TIME_FORMAT, " month ",
2);
String addDay = DateFormatUtils.addDate(currentFormatDateTime,
DateFormatUtils.
DATE_TIME_FORMAT, " day ", 2);
String addHour = DateFormatUtils.addDate(currentFormatDateTime,
DateFormatUtils.
DATE_TIME_FORMAT, " hour ", 2);
String addMinute = DateFormatUtils.addDate(currentFormatDateTime,
DateFormatUtils.DATE_TIME_FORMAT, " minute ", 2);
String minusWeek = DateFormatUtils.minusDate(currentFormatDateTime,
DateFormatUtils.DATE_TIME_FORMAT, " week ", 2);
String minusSecond = DateFormatUtils.minusDate(currentFormatDateTime,
DateFormatUtils.DATE_TIME_FORMAT, " second ", 2);
System.out.println(" Test 17: now -- " + currentFormatDateTime +
" \n\t add 2 years: " + addYear +
" \n\t add 2 month: " + addMonth +
" \n\t add 2 days: " + addDay +
" \n\t add 2 hours: " + addHour +
" \n\t add 2 minutes: " + addMinute +
" \n\t minus 2 weeks: " + minusWeek +
" \n\t minus 2 seconds: " + minusSecond);
System.out.println(" Test 18: " +
" \n\t addMonth compare to addYear : " +
DateFormatUtils.compareDate(addMonth, addYear,
DateFormatUtils.DATE_TIME_FORMAT) +
" \n\t addMonth compare to addDay : " +
DateFormatUtils.compareDate(addMonth, addDay,
DateFormatUtils.DATE_TIME_FORMAT) +
" \n\t addMonth compare to addMonth : " +
DateFormatUtils.compareDate(addMonth, addMonth,
DateFormatUtils.DATE_TIME_FORMAT));
String firstDayInMonth = DateFormatUtils.getFirstDayInMonth(
currentFormatDateTime);
System.out.println(" Test 19: the first day in month is " +
firstDayInMonth);
String lastDayInMonth = DateFormatUtils.getLastDayInMonth(
currentFormatDateTime);
System.out.println(" Test 20: the last day in month is " +
lastDayInMonth);
String firstDayInWeek = DateFormatUtils.getFirstDayInWeek(
currentFormatDateTime);
System.out.println(" Test 21: the first day in week is " +
firstDayInWeek);
String lastDayInWeek = DateFormatUtils.getLastDayInWeek(
currentFormatDateTime);
System.out.println(" Test 21: the last day in week is " +
lastDayInWeek);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -