Hey guys in this short article, you will learn how to convert the current date and time to different formats in String
Read More:
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
- Check the Spring Boot JdbcTemplate Tutorials
To convert Date to different formats in String we will use SimpleDateFormat()
object.
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Convert date to dd/MM/yyyy format
public class FormatDate {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatDate.format(date));
}
}
Output:
03/01/2022
Convert date to dd/MM/yyyy hh:mm:ss format
public class FormatDate {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatDate.format(date));
}
}
Output:
03/01/2022 04:15:34
That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.