org.onetwo.common.date.NiceDate Maven / Gradle / Ivy
package org.onetwo.common.date;
import java.time.Duration;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.onetwo.common.date.DateUtils.DateType;
import org.onetwo.common.utils.Assert;
public class NiceDate {
public static NiceDate New(){
return new NiceDate();
}
public static NiceDate New(Date date){
Assert.notNull(date);
return new NiceDate(date);
}
public static NiceDate New(String dateStr){
Date date = DateUtils.parse(dateStr);
return New(date);
}
public static NiceDate New(String dateStr, String format){
Date date = DateUtils.parseByPatterns(dateStr, format);
return New(date);
}
public static enum DatePhase {
morning(6, 12),
noon(12, 14),
afternoon(14, 18),
night(18, 24),
deepnight(0, 6);
private int start;
private int end;
/*****
*
* @param start include
* @param end exclude
*/
private DatePhase(int start, int end) {
this.start = start;
this.end = end;
}
public boolean inPhase(int hour){
if(hour>=start && hourdate.getTimeInMillis();
}
public boolean isEquals(NiceDate date){
return getTimeInMillis()==date.getTimeInMillis();
}
public Duration awayFrom(NiceDate date){
long distance = date.getTimeInMillis() - this.getTimeInMillis();
Duration duration = Duration.ofMillis(Math.abs(distance));
return duration;
}
public static void main(String[] args){
NiceDate date = new NiceDate();
System.out.println(date.getDatePhase());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy