me.wuwenbin.lang.web.WebDate Maven / Gradle / Ivy
package me.wuwenbin.lang.web;
import org.springframework.util.Assert;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class WebDate {
public final String PATTERN_STANDARD = "yyyy-MM-dd HH:mm:ss";
public final String PATTERN_DATE = "yyyy-MM-dd";
public String timestamp2String(Timestamp timestamp, String pattern) {
if (timestamp == null) {
throw new IllegalArgumentException("timestamp null illegal");
}
if (pattern == null || pattern.equals("")) {
pattern = PATTERN_STANDARD;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(new Date(timestamp.getTime()));
}
public String date2String(Date date, String pattern) {
if (date == null) {
throw new IllegalArgumentException("timestamp null illegal");
}
if (pattern == null || pattern.equals("")) {
pattern = PATTERN_STANDARD;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
public Timestamp currentTimestamp() {
return new Timestamp(new Date().getTime());
}
public String currentTimestamp2String(String pattern) {
return timestamp2String(currentTimestamp(), pattern);
}
public Timestamp string2Timestamp(String strDateTime, String pattern) {
if (strDateTime == null || strDateTime.equals("")) {
throw new IllegalArgumentException("Date Time Null Illegal");
}
if (pattern == null || pattern.equals("")) {
pattern = PATTERN_STANDARD;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = null;
try {
date = sdf.parse(strDateTime);
} catch (ParseException e) {
throw new RuntimeException(e);
}
return new Timestamp(date.getTime());
}
public Date string2Date(String strDate, String pattern) {
if (strDate == null || strDate.equals("")) {
throw new RuntimeException("str date null");
}
if (pattern == null || pattern.equals("")) {
pattern = PATTERN_DATE;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = null;
try {
date = sdf.parse(strDate);
} catch (ParseException e) {
throw new RuntimeException(e);
}
return date;
}
public String stringToYear(String strDest) {
if (strDest == null || strDest.equals("")) {
throw new IllegalArgumentException("str dest null");
}
Date date = string2Date(strDest, PATTERN_DATE);
Calendar c = Calendar.getInstance();
c.setTime(date);
return String.valueOf(c.get(Calendar.YEAR));
}
public String stringToMonth(String strDest) {
if (strDest == null || strDest.equals("")) {
throw new IllegalArgumentException("str dest null");
}
Date date = string2Date(strDest, PATTERN_DATE);
Calendar c = Calendar.getInstance();
c.setTime(date);
// return String.valueOf(c.get(Calendar.MONTH));
int month = c.get(Calendar.MONTH);
month = month + 1;
if (month < 10) {
return "0" + month;
}
return String.valueOf(month);
}
public String stringToDay(String strDest) {
if (strDest == null || strDest.equals("")) {
throw new IllegalArgumentException("str dest null");
}
Date date = string2Date(strDest, PATTERN_DATE);
Calendar c = Calendar.getInstance();
c.setTime(date);
// return String.valueOf(c.get(Calendar.DAY_OF_MONTH));
int day = c.get(Calendar.DAY_OF_MONTH);
if (day < 10) {
return "0" + day;
}
return "" + day;
}
public Date getFirstDayOfMonth(Calendar c) {
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = 1;
c.set(year, month, day, 0, 0, 0);
return c.getTime();
}
public Date getLastDayOfMonth(Calendar c) {
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = 1;
if (month > 11) {
month = 0;
year = year + 1;
}
c.set(year, month, day - 1, 0, 0, 0);
return c.getTime();
}
public String date2GregorianCalendarString(Date date) {
if (date == null) {
throw new IllegalArgumentException("Date is null");
}
long tmp = date.getTime();
GregorianCalendar ca = new GregorianCalendar();
ca.setTimeInMillis(tmp);
try {
XMLGregorianCalendar t_XMLGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(ca);
return t_XMLGregorianCalendar.normalize().toString();
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
throw new IllegalArgumentException("Date is null");
}
}
public boolean compareDate(Date firstDate, Date secondDate) {
if (firstDate == null || secondDate == null) {
throw new RuntimeException();
}
String strFirstDate = date2String(firstDate, "yyyy-MM-dd");
String strSecondDate = date2String(secondDate, "yyyy-MM-dd");
if (strFirstDate.equals(strSecondDate)) {
return true;
}
return false;
}
public Date getStartTimeOfDate(Date currentDate) {
Assert.notNull(currentDate);
String strDateTime = date2String(currentDate, "yyyy-MM-dd") + " 00:00:00";
return string2Date(strDateTime, "yyyy-MM-dd hh:mm:ss");
}
public Date getEndTimeOfDate(Date currentDate) {
Assert.notNull(currentDate);
String strDateTime = date2String(currentDate, "yyyy-MM-dd") + " 59:59:59";
return string2Date(strDateTime, "yyyy-MM-dd hh:mm:ss");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy