All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.xphsc.util.DateTimeUtil Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
package com.github.xphsc.util;


import com.github.xphsc.date.DateFormat;
import org.joda.time.DateTime;
import java.util.Date;



public class DateTimeUtil {

    private static class SingletonDateTime {
        static final  DateTime INSTANCE = new DateTime();;
    }

    public static DateTime dateTime() {
        return SingletonDateTime.INSTANCE;
    }


    public  static String lastYearStartDate(){
        String  date=  String.format(dateTime().now().plusYears(-1).dayOfYear().withMinimumValue()
                .toString(DateFormat.DATE_FORMAT_DAY));
        return date;
    }

    public  static String lastYearStartDate(String dateFormat){
        String  date=  String.format(dateTime().now().plusYears(-1).dayOfYear().withMinimumValue()
                .toString(dateFormat));
        return date;
    }

    public  static String lastYearendDate(){
        String  date=  String.format(dateTime().now().plusYears(-1).dayOfYear().withMaximumValue()
                .toString(DateFormat.DATE_FORMAT_DAY));
        return date;
    }

    public  static String lastYearendDate(String dateFormat){
        String  date=  String.format(dateTime().now().plusYears(-1).dayOfYear().withMaximumValue()
                .toString(dateFormat));
        return date;
    }

    public static  String thisYearEndDate(){
        String  date=  String.format(dateTime().now().dayOfYear().withMaximumValue()
                .toString(DateFormat.DATE_FORMAT_DAY));
        return date;
    }

    public static  String thisYearEndDate(String dateFormat){
        String  date=  String.format(dateTime().now().dayOfYear().withMaximumValue()
                .toString(dateFormat));
        return date;
    }
    public static String thisYearStartDate(){
        String  date=  String.format(dateTime().now().dayOfYear().withMinimumValue()
                .toString(DateFormat.DATE_FORMAT_DAY));
        return date;
    }

    public static String thisYearStartDate(String dateFormat){
        String  date=  String.format(dateTime().now().dayOfYear().withMinimumValue()
                .toString(dateFormat));
        return date;
    }



    public static String  nextYearStartDate(){
        String  date=  String.format(dateTime().now().plusYears(1).dayOfYear().withMinimumValue()
                .toString(DateFormat.DATE_FORMAT_DAY));
        return date;
    }

    public static String  nextYearStartDate(String dateFormat){
        String  date=  String.format(dateTime().now().plusYears(1).dayOfYear().withMinimumValue()
                .toString(dateFormat));
        return date;
    }

    public static String nextYearEndDate(){
        String  date=  String.format(dateTime().now().plusYears(1).dayOfYear().withMaximumValue()
                .toString(DateFormat.DATE_FORMAT_DAY));
        return date;
    }

    public static String nextYearEndDate(String dateFormat){
        String  date=  String.format(dateTime().now().plusYears(1).dayOfYear().withMaximumValue()
                .toString(dateFormat));
        return date;
    }

    public static String dateTimeToString(DateTime d){
        String date = d.toString(DateFormat.DATE_FORMAT_DAY);
        return date;
    }

    public static String dateTimeToString(DateTime d, String dateFormat){
        String date="";
        if (RegexUtil.isMatched(dateFormat, DateFormat.DATE_FORMAT_DAY)) {
            date =  d.toString(DateFormat.DATE_FORMAT_DAY);
        }
        if (RegexUtil.isMatched(dateFormat, DateFormat.DATE_FORMAT_DAY_2)) {
            date = d.toString(DateFormat.DATE_FORMAT_DAY_2);
        }
        if (RegexUtil.isMatched(dateFormat, DateFormat.TIME_FORMAT_SEC)) {
            date = d.toString(DateFormat.TIME_FORMAT_SEC);
        }
        if (RegexUtil.isMatched(dateFormat,DateFormat.DATE_FORMAT_SEC)) {
            date = d.toString(DateFormat.DATE_FORMAT_SEC);
        }
        if (RegexUtil.isMatched(dateFormat, DateFormat.DATE_FORMAT_MSEC)) {
            date = d.toString(DateFormat.DATE_FORMAT_MSEC);
        }
        if (RegexUtil.isMatched(dateFormat,DateFormat.DATE_FORMAT_MSEC_T)) {
            date = d.toString(DateFormat.DATE_FORMAT_MSEC_T);
        }
        if (RegexUtil.isMatched(dateFormat,DateFormat.DATE_FORMAT_MSEC_T_Z)) {
            date = d.toString(DateFormat.DATE_FORMAT_MSEC_T_Z);
        }
        return date;
    }

    public static String dateTimeToTimeString(DateTime d){
        String date = d.toString(DateFormat.DATE_FORMAT_MSEC);
        return date;
    }

    public static Long dateTimeToLong(DateTime d){
        long date = d.getMillis();
        return date;
    }

    public static Long dateTimeHMS$ToZero(DateTime d){
        long  date = d.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withSecondOfMinute(0).withMillisOfSecond(0).getMillis();
        return date;
    }

    public static Long dateTimeHMS$ToZero(){
        long  date =dateTime().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withSecondOfMinute(0).withMillisOfSecond(0).getMillis();
        return date;
    }

    public static DateTime dateTimePlusDays(DateTime d,Integer plusDays){
        DateTime  dateTime =d.plusDays(plusDays);
        return dateTime;
    }

    public static Long dateTimePlusDaysHMS$ToZero(DateTime d,Integer plusDays){
        long  date =d.plusDays(plusDays).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withSecondOfMinute(0).withMillisOfSecond(0).getMillis();
        return date;
    }

    public static String longTimeToString(Long time){
        DateTime datetime= dateTime().withMillis(time);
        String date=datetime.toString(DateFormat.DATE_FORMAT_MSEC);
        return date;
    }

    public static String longTimeToString(Long time,String dateFormat){
        DateTime datetime= dateTime().withMillis(time);
        String date=datetime.toString(dateFormat);
        return date;
    }

    public static DateTime StringTimeToDateTime(String time){
        DateTime date= dateTime().parse(time);
        return date;
    }

    public static String StringTimeToDateFormat(String time,String dateFormat){
        DateTime dateTime= dateTime().parse(time);
        String date=dateTime.toString(dateFormat);
        return date;
    }

    public static Date dateTimeToDate(DateTime d){
        long time = d.getMillis();
        Date date=new Date();
        date.setTime(time);
        return date;
    }

    public static DateTime dateToDateTime(Date d){
        long time = d.getTime();
        DateTime date=dateTime().withMillis(time);
        return date;
    }


    public static void main(String[] args)  {
        System.out.println(dateTimeToString(DateTime.now()));;
        System.out.println(nextYearStartDate());
        System.out.println(longTimeToString(new Long("1483239911061")));
        System.out.println(StringTimeToDateTime("2017-02-09"));
        System.out.println(dateTimeToDate(DateTime.now()));
        System.out.println("----------------");
        System.out.println(dateToDateTime(new Date()));
        System.out.println(dateTimeHMS$ToZero(DateTime.now()));
        System.out.println(DateTimeUtil.dateTime().now());
        System.out.println(StringTimeToDateFormat("2012-5-7",DateFormat.DATE_FORMAT_SEC));


    }
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy