📄 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 + -