Java、postgre、mysql中時間計算

 

Java中時間計算

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date now = new Date();
  System.out.println("
當前時間:" + sdf.format(now));

方法一:

  Date afterDate = new Date(now .getTime() + 300000);
  System.out.println(sdf.format(afterDate ));

  方法二:
  Calendar nowTime = Calendar.getInstance();
  nowTime.add(Calendar.MINUTE, 5);
  System.out.println(sdf.format(nowTime.getTime()));

 

1. long字符串轉換成yyyy-MM-dd HH:mm:ss格式輸出

Date date = new Date();

 String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);

 

  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3. //long字符串轉換成格式時間輸出  
  4. public class LongToString {  
  5. public static void main(String argsp[]){  
  6.     String time="1256006105375";  
  7.   
  8.     Date date=new Date(Long.parseLong(time));  
  9.     SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  10.     time=formatter.format(date);  
  11.     System.out.println(time);  
  12. }  
  13. }  

2. 字符串轉換成時間

  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3.   
  4. import ognl.ParseException;  
  5.   
  6. public class StringToDate {  
  7. public static void main(String argsp[]) throws Exception{  
  8.     String time="2010-11-20 11:10:10";  
  9.   
  10.     Date date=null;  
  11.     SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  12.     date=formatter.parse(time);  
  13.     System.out.println(date);  
  14. }  
  15. }  

 

Date todayTimeDate = DateFormatUtils.toDate(todayTime, DateFormatUtils.FORMAT_COMMON_DATE_TIME);

mysql求時間差值的秒值

select timestampdiff(SECOND,createtime,NOW()) from table1;

 

postgresql獲取當前時間的方式

1.now()

返回值:當前年月日、時分秒,且秒保留6位小數。

2.current_timestamp

返回值:當前年月日、時分秒,且秒保留6位小數。(同上)

申明:nowcurrent_timestamp幾乎沒區別,返回值相同,建議用now

3.current_time

返回值:時分秒,秒最高精確到6位

4.current_date

返回值:年月日

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