Posts

Showing posts with the label String to Date

Java - How to convert String to Date

Image
In this section, we will show you h ow to  convert String to java.util.Date  in Java. Refer to table below for some of the common date and time patterns used in java.text. SimpleDateFormat , refer to this  JavaDoc 1. String = 11-July-2023 Main.java import java.text.ParseException ; import java.text.SimpleDateFormat ; import java.util.Date ; public class Main { public static void main ( String [] argv) { SimpleDateFormat formatter = new SimpleDateFormat( "dd-MMM-yyyy" ); String dateInString = "11-July-2023" ; try { Date date = formatter .parse( dateInString ); System . out .println( date ); System . out .println( formatter .format( date )); } catch ( ParseException e) { e.printStackTrace(); } } } Console Output: Tue Jul 11 00:00:00 IST 2023 11-Jul-2023   2. String = 11/09/2023 Main.java import java.text.ParseException ; import java.text.SimpleDateFormat ;