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);
- import java.text.SimpleDateFormat;
- import java.util.Date;
- //將long字符串轉換成格式時間輸出
- public class LongToString {
- public static void main(String argsp[]){
- String time="1256006105375";
- Date date=new Date(Long.parseLong(time));
- SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- time=formatter.format(date);
- System.out.println(time);
- }
- }
2. 字符串轉換成時間
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import ognl.ParseException;
- public class StringToDate {
- public static void main(String argsp[]) throws Exception{
- String time="2010-11-20 11:10:10";
- Date date=null;
- SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- date=formatter.parse(time);
- System.out.println(date);
- }
- }
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位小數。(同上)
申明:now和current_timestamp幾乎沒區別,返回值相同,建議用now。
3.current_time
返回值:時分秒,秒最高精確到6位
4.current_date
返回值:年月日