package com.base.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
// public static String nowDate(){
// Calendar now = Calendar.getInstance();
// return null;
// }
/**
* 比较两个日期的大小
* @author Chao
* format 时间格式 "yyyy-MM" "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
* 返回 i 1-time1大于time2 -1-time2大于time1 0-相等
*/
public static int complateTime(String time1,String time2,String format) {
int i = 100 ;
DateFormat df = new SimpleDateFormat(format);
//获取Calendar实例
Calendar currentTime = Calendar.getInstance();
Calendar compareTime = Calendar.getInstance();
try {
//把字符串转成日期类型
currentTime.setTime(df.parse(time1));
compareTime.setTime(df.parse(time2));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//利用Calendar的方法比较大小
if (currentTime.compareTo(compareTime) > 0) {
return 1;
}else if(currentTime.compareTo(compareTime) < 0){
return -1;
}else if(currentTime.compareTo(compareTime) == 0) {
return 0;
}
return i;
}
/**
* 计算日期几天前或者几天后时间
* @param time 传入的时间
* @param state 1-加年 2-加月 3-加日
* @param day 增加或者减少的数值
* @param format 日期格式
* @return 计算后的时间 "yyyy-MM-dd" "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
* @throws ParseException
*/
public static String getDate(String time,int state,int day,String format) throws ParseException{
String d = time;
DateFormat df = new SimpleDateFormat(format);/*** 加一天*/
Date dd = df.parse(d);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dd);
if(state==3){
calendar.add(Calendar.DATE, day);
}
if(state==2){
calendar.add(Calendar.MONTH, day);
}
if(state==1){
calendar.add(Calendar.YEAR, day);
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateStr = sdf.format(calendar.getTime());
return dateStr;
}
/**
* 通过设置时间格式获取当前时间
* @param 时间格式 "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
* @return string
*/
public static String getCurrentTime(String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = new Date();
String formatDate = sdf.format(date);
return formatDate;
}
/**
* 字符串时间转时间类型 固定日期格式
* @param text 字符串时间 ,format "yyyy-MM-dd HH:mm:ss"
* @return Date
* @throws Exception
*/
public static Date getStringToDate(String time,String format) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat(format);//格式时间对象
Date date = sdf.parse(time);
return date;
}
/**
* 获取时间戳
* @param time
* @param format
* @return
* @throws Exception
*/
public static long getTimeStampByTime(String time,String format) throws Exception{
return getStringToDate(time,format).getTime();
}
/**
* 时间String转Date
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 获取两个时间的间隔秒数
* @param time1
* @param time2
* @param format
* @return
* @throws ParseException
*/
public static long getDiffTime(String time1,String time2,String format) throws ParseException{
SimpleDateFormat dfs = new SimpleDateFormat(format);
Date begin=dfs.parse(time1);
Date end = dfs.parse(time2);
long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
// long min=between/60;
return between;
}
public static void main(String[] args) throws ParseException {
String aString = nowTime("yyyy-MM-dd HH:mm:ss");
String test1 = "00:00:01";
String test2 = "11:11:12";
try {
System.out.println(getTimeStampByTime(test1,"HH:mm:ss"));
System.out.println(getTimeStampByTime(test2,"HH:mm:ss"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}