String字符串和时间的转换以及时间的计算
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTimeUtils {
public static String getCurrentTimeMS(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String currentTime = format.format(Calendar.getInstance().getTime());
return currentTime;
}
public static long compareTime(String beginTime,String endTime){
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
long between = 0;
try {
Date begin = dfs.parse(beginTime);
Date end = dfs.parse(endTime);
between = (end.getTime() - begin.getTime());
} catch (Exception ex) {
ex.printStackTrace();
}
return between;
}
public static String dayFormat(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
public static String dayFormatSimple(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(date);
}
public static String secondFormat(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
public static String msecFormat(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return sdf.format(date);
}
public static String msecSimpleFormat(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(date);
}
public static String hmsFormat(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
return sdf.format(date);
}
public static Date strToDate(String text) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static String strToDateToStr(String text,String pattern){
SimpleDateFormat sdfPattern = new SimpleDateFormat(pattern);
Date date = null;
try {
date = sdfPattern.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
String result = sdfPattern.format(date);
return result;
}
public static java.sql.Date strToSqlDate(String text) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date date1 = new java.sql.Date(date.getTime());
return date1;
}
public static Date strToDates(String text) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = null;
try {
date = sdf.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static Date yyyyMMddHHmmssToDate(String text) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = null;
try {
date = sdf.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static java.sql.Timestamp strToDatesSql(String text) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
java.sql.Timestamp date1 = null;
try {
date = sdf.parse(text);
date1 = new java.sql.Timestamp(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return date1;
}
public static Date getDateAfter(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}
public static Date getDateBefore(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
return now.getTime();
}
public static int subtractDate(Date endTime,Date startTime){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(startTime);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(endTime);
int day1= cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
if(year1 != year2)
{
int timeDistance = 0 ;
for(int i = year1 ; i < year2 ; i ++)
{
if(i%4==0 && i%100!=0 || i%400==0)
{
timeDistance += 366;
}
else
{
timeDistance += 365;
}
}
return timeDistance + (day2-day1) ;
}
else
{
System.out.println("判断day2 - day1 : " + (day2-day1));
return day2-day1;
}
}
public static String getSpecifiedDayBefore(String specifiedDay){
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-1);
String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}
public static String getSpecifiedDayAfter(String specifiedDay){
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day+1);
String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayAfter;
}
public static String dateToString(Date date, String pattern) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
return simpleDateFormat.format(date);
}
public static Date strToDate(String dateStr, String pattern) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = null;
try {
date = simpleDateFormat.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static long compareTime(Date beginTime, Date endTime) {
long between = 0;
try {
between = (endTime.getTime() - beginTime.getTime());
} catch (Exception ex) {
ex.printStackTrace();
}
return between;
}
public static String theDayBefore(Date date,int n,String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, n);
Date lastDay = calendar.getTime();
return sdf.format(lastDay);
}
public static String theMonthBefore(Date date,int n,String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, n);
Date lastDay = calendar.getTime();
return sdf.format(lastDay);
}
public static String theYearBefore(Date date, int n, String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, n);
Date lastDay = calendar.getTime();
return sdf.format(lastDay);
}
public synchronized static long aLong(Date date){
long beginMillisecond =date.getTime();
return beginMillisecond;
}
public static Date getMonthFirstDay(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.add(Calendar.MONTH,0);
return calendar.getTime();
}
}