Java Timestamp Conversions

Understanding Timestamps: In Java, a timestamp is a long integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 GMT. This is known as the Unix Epoch time. It's important to understand this format as it's used in many Java applications.

Converting Timestamps to Dates: To convert a timestamp to a date object in Java, you can use the SimpleDateFormat class. This class allows you to specify the format of the date and time values. For example, to convert a timestamp to a date object in the format "dd/MM/yyyy", you can use the following code:
1 2 3 4long timestamp = 1615225817878L; Date date = new Date(timestamp); SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String formattedDate = formatter.format(date); System.out.println(formattedDate);
This will output: "08/03/2021".
Converting Dates to Timestamps: To convert a date object to a timestamp in Java, you can use the getTime() method of the Date class. This method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT. For example
1 2 3 4String dateString = "08/03/2021"; SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Date date = formatter.parse(dateString); long timestamp = date.getTime(); System.out.println(timestamp);
This will output: "1615190400000".
Handling Timezones: When working with timestamps in Java, it's important to consider timezones. By default, Java uses the system's timezone, but you can specify a specific timezone using the TimeZone class. For example, to use the timezone "America/Los_Angeles", you can use the following code:
1 2 3 4TimeZone timeZone = TimeZone.getTimeZone("America/Los_Angeles"); Calendar calendar = Calendar.getInstance(timeZone); long timestamp = calendar.getTimeInMillis(); System.out.println(timestamp);
This will output the current timestamp in the "America/Los_Angeles" timezone.

Few more examples of different types of timestamp conversions in Java:

Converting Timestamp to Date
1 2 3 4 5 6 7 8 9 10 11import java.sql.Timestamp; import java.util.Date; public class TimestampToDateExample { public static void main(String[] args) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); Date date = new Date(timestamp.getTime()); System.out.println("Timestamp: " + timestamp); System.out.println("Date: " + date); } }
Converting Date to Timestamp
1 2 3 4 5 6 7 8 9 10 11import java.sql.Timestamp; import java.util.Date; public class DateToTimestampExample { public static void main(String[] args) { Date date = new Date(); Timestamp timestamp = new Timestamp(date.getTime()); System.out.println("Date: " + date); System.out.println("Timestamp: " + timestamp); } }
Converting String to Timestamp
1 2 3 4 5 6 7 8 9 10 11 12 13 14import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; public class StringToTimestampExample { public static void main(String[] args) throws ParseException { String dateString = "2022-03-08 12:00:00"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = dateFormat.parse(dateString); Timestamp timestamp = new Timestamp(date.getTime()); System.out.println("Date String: " + dateString); System.out.println("Timestamp: " + timestamp); } }
Converting Timestamp to String
1 2 3 4 5 6 7 8 9 10 11 12import java.sql.Timestamp; import java.text.SimpleDateFormat; public class TimestampToStringExample { public static void main(String[] args) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String dateString = dateFormat.format(timestamp); System.out.println("Timestamp: " + timestamp); System.out.println("Date String: " + dateString); } }
© 2024 made by www.epochconverter.io