Q:

How to convert Date string to Date format in Java?

belongs to collection: Java Date and Time Programs

0

How to convert Date string to Date format in Java?

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

// Java program to get current 
// system date and time

import java.text.SimpleDateFormat;
import java.util.*;

public class ConvDateString2DatePrg {
  public static void main(String args[]) {
    try {
      //define date format to take input
      SimpleDateFormat dateF = new SimpleDateFormat("dd/MM/yyyy");

      Scanner sc = new Scanner(System.in); //string object
      String dtString = "";

      System.out.print("Enter date in dd/MM/yyyy format:");
      dtString = sc.nextLine();

      //convert input date string into Date
      Date dt = dateF.parse(dtString);

      System.out.print("Entered Date is: " + dt.toString());
    } catch (Exception e) {
      System.out.println("Exception is: " + e.toString());
    }
  }
}

Output

Enter date in dd/MM/yyyy format:21/09/2008
Entered Date is: Sun Sep 21 00:00:00 IST 2008

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Date and Time Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to get current system date and time in Java?... >>
<< Comparing Date using Date.before() and Date.after(...