com.beginner.core.utils.DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.beginner.core.utils.DateUtil.java

Source

/*
 * Copyright 2015-9999 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beginner.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.junit.Assert;
import org.junit.Test;

/**
 * <b>??</b>DateUtil<br/>
 * <b>??</b><br/>
 * <b></b>Hsiao Lin Studio<br/>
 * <b></b><br/>
 * <b></b>20150521 ?6:18:18<br/>
 * <b></b><br/>
 * @version 1.0.0<br/>
 */
public class DateUtil extends DateUtils {

    public static String[] PARSE_PATTERNS = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
            "yyyy.MM.dd HH:mm", "yyyy.MM" };

    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return DateUtils.parseDate(str.toString(), PARSE_PATTERNS);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * ????
     * @param strDate    ?
     * @return String    ??
     * @since          1.0.0
     */
    public static String getTimes(String strDate) {
        String resultTimes = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date now;

        try {
            now = new Date();
            java.util.Date date = df.parse(strDate);
            long times = now.getTime() - date.getTime();
            long day = times / (24 * 60 * 60 * 1000);
            long hour = (times / (60 * 60 * 1000) - day * 24);
            long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long sec = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

            StringBuffer sb = new StringBuffer();
            if (hour > 0) {
                sb.append(hour + "??");
            } else if (min > 0) {
                sb.append(min + "?");
            } else {
                sb.append(sec + "?");
            }
            resultTimes = sb.toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return resultTimes;
    }

    /**
     * yyyy-MM-dd HH:mm:ss?
     * @param date    Date
     * @return       String yyyy-MM-dd HH:mm:ss
     * @since       1.0.0
     */
    public static String date2Str(Date date) {
        return date2Str(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * ?format?
     * @param date       Date
     * @param format    ?
     * @return String    ??
     * @since          1.0.0
     */
    public static String date2Str(Date date, String format) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        } else {
            return "";
        }
    }

    /**
     * yyyy-MM-dd HH:mm:ss?
     * @param date    
     * @return       Date
     * @since       1.0.0
     */
    public static Date str2Date(String date) {
        if (Tools.isNotEmpty(date)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                return sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return new Date();
        } else {
            return null;
        }
    }

    /**
     * DateTime??
    */
    @Test
    public void testChange() {
        DateTime dateTime = new DateTime(2014, 1, 2, 10, 11, 12);

        //123?
        Assert.assertEquals(new DateTime(2015, 3, 5, 10, 11, 12), dateTime.plusYears(1).plusMonths(2).plusDays(3));
    }

    /**
     * DateTime
    */
    @Test
    public void testSpecific() {
        DateTime dateTime = new DateTime(2014, 12, 2, 8, 12, 45, 666);

        //?13
        Assert.assertEquals(new DateTime(2014, 12, 2, 13, 12, 45, 666), dateTime.withHourOfDay(13));
    }

    /**
     * DateTime
    */
    @Test
    public void testToDateToString() {
        String str = "2014-12-02 08:12:45";
        DateTime dateTime = new DateTime(2014, 12, 2, 8, 12, 45);

        //DateTime
        DateTime parsedDateTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime(str);
        Assert.assertEquals(dateTime, parsedDateTime);

        //DateTime?
        String str01 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").print(dateTime);
        Assert.assertEquals(str, str01);

        String str02 = dateTime.toString("yyyy-MM-dd HH:mm:ss");
        Assert.assertEquals(str, str02);

    }

    /**
     * DateTime
    */
    @Test
    public void testTruncateTo() {
        DateTime dateTime = new DateTime(2014, 12, 2, 8, 12, 45, 666);

        //
        DateTime tillDay = dateTime.dayOfMonth().roundFloorCopy();
        Assert.assertEquals(new DateTime(2014, 12, 2, 0, 0, 0), tillDay);

        //
        DateTime tillSecond = dateTime.secondOfMinute().roundFloorCopy();
        Assert.assertEquals(new DateTime(2014, 12, 2, 8, 12, 45), tillSecond);

    }

    /**
     * ?23:59:59
    */
    @Test
    public void testWith() {
        DateTime dateTime = new DateTime(2014, 12, 2, 8, 12, 45);

        //?

        //23:59:59
        Assert.assertEquals(new DateTime(2014, 12, 2, 23, 59, 59),
                dateTime.withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59));

        //??
        Assert.assertEquals(new DateTime(2014, 12, 2, 23, 59, 59), dateTime.secondOfDay().withMaximumValue());

        //00:00:00
        Assert.assertEquals(new DateTime(2014, 12, 2, 0, 0, 0), dateTime.secondOfDay().withMinimumValue());
    }
}