Java Convert String to Datetime using SimpleDateFormat





Hey guys in this short article, you will learn how to convert the String into datetime with time zone in Java.

Read More:

To convert String into date we need to use parse() method on the SimpleDateFormat() object.

Date d = simpleDateFormat.parse("2021-12-31");

Convert date to dd/MM/yyyy format


public class ConvertStringToDate {
	
	public static void main(String[] args) {
		
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		String date = "2021-12-31";
		
		Date t;
		try {
			t = ft.parse(date);
			System.out.println(t);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}
}

Output:

Fri Dec 31 00:00:00 IST 2021

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.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply