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

com.datastax.util.lang.DateUtil Maven / Gradle / Ivy

The newest version!
package com.datastax.util.lang;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * UserItem: Anders Hong
 * Date: 10/11/13
 * Time: 5:17 PM
 */
public class DateUtil {
    public static void main(String[] args){
        Date endDate=new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(endDate);

        c.add(Calendar.DATE,-3);
        Date startDate=c.getTime();

        System.out.println(startDate.before(endDate));

        List ranges=getRangeTime(startDate,endDate,3);
        for(Date date : ranges){
            System.out.println(date);
        }
    }

    public static Date getPreTime(Date date,int group){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.MINUTE, 0);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int distance = hour%group;
        c.add(Calendar.HOUR_OF_DAY, -distance);
        return c.getTime();
    }

    public static Date getNextTime(Date date,int group){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.MINUTE, 0);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int distance = group-hour%group;
        c.add(Calendar.HOUR_OF_DAY, distance);
        return c.getTime();
    }

    public static List getRangeTime(Date startDate,Date endDate,int group){
        List range=new ArrayList<>();

        startDate=getPreTime(startDate,group);
        endDate=getNextTime(endDate,group);

        for(int i=0;i0){
                String todayMin=shareTimeFormat.substring(shareTimeFormat.indexOf(" ")+1, shareTimeFormat.length());
                shareTime="今天 "+todayMin;
            }
            else if(day>0){
                String today=shareTimeFormat.substring(shareTimeFormat.indexOf("年")+1,shareTimeFormat.length());
                shareTime=today;
            }
            return shareTime;
        }
        else{
            return shareTimeFormat;
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy