?? dateformatutilstest.java
字號:
/**
* 時間及時間格式處理測試
* */
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("系統的當前時間(毫秒): " + currentTimeMillis);
Date currentDate = DateFormatUtils.getCurrentDate();
System.out.println("系統的當前時間: " + currentDate);
String currentFormatDate = DateFormatUtils.getCurrentFormatDate();
System.out.println("系統的當前日期(默認格式): " + currentFormatDate);
String currentFormatDateTime = DateFormatUtils.getCurrentFormatDateTime();
System.out.println("系統的當前日期時間(默認格式): " + currentFormatDateTime);
String currentCustomFomatDateTime12 = DateFormatUtils.
getCurrentCustomFormatDateTime("yyyy-MM-dd HH:mm ");
System.out.println("系統的當前日期時間(指定格式返回): " + 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("按照默認格式轉換為Date: " + strToDate);
Date strToDateTime = DateFormatUtils.parseStrToDateTime(
currentFormatDateTime);
System.out.println("按照默認格式轉換為Date: " + strToDateTime);
Calendar cal = DateFormatUtils.parseStrToCalendar(currentFormatDateTime);
System.out.println("按照默認格式轉換為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("日期字符串轉換成日期時間字符串: " + currentFormatDate +
" = " + 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: " + strToDate +
", After Add 30 Days, \n\t The Time Is: " +
addDays);
Date minusDays = DateFormatUtils.minusDays(addDays, 35);
System.out.println("Test 16: " + 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("按時間格式相減: " + 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 Eeeks: " + minusWeek +
" \n\t Minus 2 Deconds: " + minusSecond);
System.out.println("日期大小比較: " +
" \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("本月的第一天: " + firstDayInMonth);
String lastDayInMonth = DateFormatUtils.getLastDayInMonth(
currentFormatDateTime);
System.out.println("本月的最后一天: " + lastDayInMonth);
String firstDayInWeek = DateFormatUtils.getFirstDayInWeek(
currentFormatDateTime);
System.out.println("本周的第一天: " + firstDayInWeek);
String lastDayInWeek = DateFormatUtils.getLastDayInWeek(
currentFormatDateTime);
System.out.println("本周的最后一天: " + lastDayInWeek);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -