We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes.
Approach: Using SimpleDateFormat
Java
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateToString {public static void main(String[] args) throws ParseException {String pattern = "MM/dd/yyyy HH:mm:ss";String strDate = "04/02/2021 22:57:29";SimpleDateFormat df = new SimpleDateFormat(pattern);Date today =df.parse(strDate);System.out.println("Today is: " + today);}}
Output:
Today is: Fri Apr 02 22:57:29 IST 2021
No comments:
Post a Comment