java timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
//2016-11-16 06:43:19.77
Copy
java timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
//2016-11-16 06:43:19.77
Copy
java timestamp
package com.mkyong.date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeStampExample {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
public static void main(String[] args) {
//method 1
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);
//method 2 - via Date
Date date = new Date();
System.out.println(new Timestamp(date.getTime()));
//return number of milliseconds since January 1, 1970, 00:00:00 GMT
System.out.println(timestamp.getTime());
//format timestamp
System.out.println(sdf.format(timestamp));
}
}
Copy
java timestamp from string
//parse ISO local date time String
LocalDateTime timestamp=LocalDateTime.parse("2007-12-03T10:15:30");
//parse custom String
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd HH:mm");
LocalDate parsedDate = LocalDate.parse("1970 01 01 00:00", formatter);
java timestamp
package com.mkyong.app;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeStampExample {
// 2021.03.24.16.34.26
private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
// 2021-03-24T16:44:39.083+08:00
private static final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// 2021-03-24 16:48:05
private static final SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
// method 1
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp); // 2021-03-24 16:34:26.666
// method 2 - via Date
Date date = new Date();
System.out.println(new Timestamp(date.getTime())); // 2021-03-24 16:34:26.666
// number of milliseconds since January 1, 1970, 00:00:00 GMT
System.out.println(timestamp.getTime()); // 1616574866666
System.out.println(sdf1.format(timestamp)); // 2021.03.24.16.34.26
System.out.println(sdf2.format(timestamp)); // 2021-03-24T16:48:05.591+08:00
System.out.println(sdf3.format(timestamp)); // 2021-03-24 16:48:05
}
}
date to timestamp java
All you need to do is change the string within the java.text.SimpleDateFormat constructor to: "MM-dd-yyyy HH:mm:ss".
Just use the appropriate letters to build the above string to match your input date.
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us