Java convert date to timestamp: In the previous article we have discussed Java Program to Convert Date to Timestamp
In this article we will see how to convert a time stamp to date.
Program To Convert Timestamp to Date
Let’s see different ways to do it.
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Method-1 : Java Program to Convert Timestamp to Date Using geTime() method
Approach :
- Take the instant timestamp through inbuild function
currentTimeMillis()
store it in a variable lets say “t
”. - Convert the timestamp format to date by assigning it Date class object. .
- Print the result .
Program :
import java.sql.Timestamp; import java.util.Date; public class Main { public static void main(String args[]) { // defining to Timestamp Timestamp t = new Timestamp(System.currentTimeMillis()); // converting to dat Date output =new Date(t.getTime()); // printing the result System.out.println("Converted date String: " + output ); } }
Output : Converted date String: Tue Sep 07 11:42:28 UTC 2021
Method-2 : Java Program to Convert Timestamp to Date Using typecasting
Approach :
- Take the instant timestamp through inbuild function
currentTimeMillis()
store it in a variable lets say “t
”. - Convert the timestamp format to date by typecasting .
- Print the result .
Program :
import java.sql.Timestamp; import java.util.Date; public class Main { public static void main(String args[]) { // defining to Timestamp Timestamp t= new Timestamp(System.currentTimeMillis()); // converting to dat Date output = t; // printing the result System.out.println("Converted date String: " + output ); } }
Output : Converted date String: 2021-09-07 11:44:48.461
Want to excel in java coding? Practice with these Java Programs examples with output and write any
kind of easy or difficult programs in the java language
Related Java Program: