常用時間格式

1、mysql更新一段時間內的隨機時間
UPDATE devicestatus SET statustime = concat(‘2020-05-0’, floor(4+rand()*6),’ ‘, floor(10+rand()*10),’:’,floor(10+rand()49),’:’,floor(10+rand()49));
2、 判斷兩個timestamp類型的大小 第二個數比第一個大 返回 true 第二個數比第一個數小 返回 false
public static Boolean compareTimeStamp(Timestamp timestamp , Timestamp timestamp2) {
Long time1 = 0l;
Long time2 = 0l;
if(timestamp != null ) {
time1 = timestamp.getTime();
}
if(timestamp2 != null) {
time2 = timestamp2.getTime();
}
if(timestamp == null) {
return true;
}
if(time1 > time2) {
return false;
}else {
return true;
}
}
3、/獲取指定年月的下月
public static String getNextMonth(String string) {
try
{
SimpleDateFormat sdf = new SimpleDateFormat (“yyyy-MM”);
Date date = sdf.parse (string);
Calendar calendar = Calendar.getInstance ();
calendar.setTime (date);
calendar.add (Calendar.MONTH, 1);
return sdf.format (calendar.getTime ());
}
catch (ParseException e)
{
e.printStackTrace ();
}
return “”;
}
4、獲取指定時間的上個月
public static String getbeforeMonth(String string) {
try
{
SimpleDateFormat sdf = new SimpleDateFormat (“yyyy-MM”);
Date date = sdf.parse (string);
Calendar calendar = Calendar.getInstance ();
calendar.setTime (date);
calendar.add (Calendar.MONTH, -1);
return sdf.format (calendar.getTime ());
}
catch (ParseException e)
{
e.printStackTrace ();
}
return “”;
}
5、指定年月日的上個月
public static String getbeforeMonth1(String date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd”);
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
calendar.add(Calendar.MONTH, -1);
return simpleDateFormat.format(calendar.getTime());
}
6、指定日期的後幾天
public static String getDateAfter(String d,int day){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd”);
Date date;
try {
date = simpleDateFormat.parse(d);
Calendar now =Calendar.getInstance();
now.setTime(date);
now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
return simpleDateFormat.format(now.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
7、獲取兩個日期的差值
public static String getTimeStampRes(Timestamp timestamp1 , Timestamp timestamp2) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm”);
String date1 = simpleDateFormat.format(timestamp1);
String date2 = simpleDateFormat.format(timestamp2);
date1 = date1 + “:00”;
date2 = date2 + “:00”;
timestamp1 = Timestamp.valueOf(date1);
timestamp2 = Timestamp.valueOf(date2);
Long res= timestamp2.getTime() - timestamp1.getTime();
Double double1 = res / (1000
60
60.0);
DecimalFormat decimalFormat = new DecimalFormat("#####################.00");
return decimalFormat.format(double1);
}

8、獲取兩個日期的差值
public static Double getTimeStampRes1(String timestamp1 , String timestamp2) {
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd”);
// String date1 = simpleDateFormat.format(timestamp1);
// String date2 = simpleDateFormat.format(timestamp2);
String date1 = timestamp1 + " 24:00:00";
String date2 = timestamp2 + " 00:00:00";
Timestamp one = Timestamp.valueOf(date1);
Timestamp two = Timestamp.valueOf(date2);
Long res= one.getTime() - two.getTime();
Double double1 = res / (10006060.0*24);
return double1;
}
9、獲取明天的時間
public static String getTomorrow(Date date) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,1);//把日期往後增加一天.整數往後推,負數往前移動
date=calendar.getTime(); //這個時間就是日期往後推一天的結果
SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);
String dateString = formatter.format(date);
return dateString;
}

 /**
  * 獲取明天的時間
  * @param stringDate : 前一天時間
  * @return
  */
 public static String getTomorrow(String  stringDate) {
	 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	 Date date = null;
	try {
		date = formatter.parse(stringDate);
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 Calendar calendar = new GregorianCalendar();
	 calendar.setTime(date);
	 calendar.add(calendar.DATE,1);//把日期往後增加一天.整數往後推,負數往前移動
	 date=calendar.getTime(); //這個時間就是日期往後推一天的結果 
	 String dateString = formatter.format(date);
	 return dateString;
 }
 10、JAVA獲取某段時間內的所有日期 
public static List < Date > findDates(Date dBegin, Date dEnd) {
    List lDate = new ArrayList();
    lDate.add(dBegin);
    Calendar calBegin = Calendar.getInstance();
    // 使用給定的 Date 設置此 Calendar 的時間
    calBegin.setTime(dBegin);
    Calendar calEnd = Calendar.getInstance();
    // 使用給定的 Date 設置此 Calendar 的時間
    calEnd.setTime(dEnd);
    // 測試此日期是否在指定日期之後
    while (dEnd.after(calBegin.getTime())) {
        // 根據日曆的規則,爲給定的日曆字段添加或減去指定的時間量
        calBegin.add(Calendar.DAY_OF_MONTH, 1);
        lDate.add(calBegin.getTime());
    }
    return lDate;
}

11、獲取某月的最後一天
public static String getLastDayOfMonth(int year,int month)
{
Calendar cal = Calendar.getInstance();
//設置年份
cal.set(Calendar.YEAR,year);
//設置月份
cal.set(Calendar.MONTH, month-1);
//獲取某月最大天數
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//設置日曆中月份的最大天數
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
12、獲取指定年月日的後幾個月的時間
public static String getAfterDateMonth(String time , int month) {
Calendar c = Calendar.getInstance();//獲得一個日曆的實例
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = null;
try{
date = sdf.parse(time);//初始日期
}catch(Exception e){

       }
       c.setTime(date);//設置日曆時間
       c.add(Calendar.MONTH,month);//在日曆的月份上增加6個月
       return sdf.format(c.getTime());
}
13、昨天的時間
public static String getYesterday(String time) {
	//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
	Calendar c = Calendar.getInstance(); 
	Date date=null; 
	try { 
	date = new SimpleDateFormat("yy-MM-dd").parse(time); 
	} 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; 
}

14、某月最後一天
public static String getMonthEnd(int year,int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate endOfMonth = yearMonth.atEndOfMonth();
LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of(“Asia/Shanghai”));
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = Date.from(zonedDateTime.toInstant());
return sdf.format(date );
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章