dateformatutilstest.java~9~

来自「很不错的结合DateTime的综合例子」· JAVA~9~ 代码 · 共 142 行

JAVA~9~
142
字号
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(" Test 10:    Calendar =  " +
                           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);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?