Sagar Zala
Sagar Zala

Reputation: 5134

How to convert string to date format

I want to convert 3/13/2014 11:38:58 AM string to date format.

I see some examples but and also implement but I don't know how to convert AM/PM to 24 hour time format.

How to make it possible ?

Upvotes: 2

Views: 226

Answers (3)

Monny
Monny

Reputation: 297

Parsing Strings into Dates: The SimpleDateFormat class has some additional methods, notably parse( ) , which tries to parse a string according to the format stored in the given SimpleDateFormat object. For example:

import java.util.*;
import java.text.*; 
public class DateDemo {
public static void main(String args[]) {
  SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd"); 

  String input = args.length == 0 ? "1818-11-11" : args[0]; 

  System.out.print(input + " Parses as "); 

  Date t; 

  try { 
      t = ft.parse(input); 
      System.out.println(t); 
  } catch (ParseException e) { 
      System.out.println("Unparseable using " + ft); 
  }
 }
}

Upvotes: 0

Abimaran Kugathasan
Abimaran Kugathasan

Reputation: 32458

Use SimpleDateFormat

Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").parse(string);

Upvotes: 1

Java Man
Java Man

Reputation: 1860

Using this you can convert your date and time..

SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
Date date_current =  new Date();
Date date_start = null;
date_start = sdf.parse("3/13/2014 11:38:58 AM");
System.out.println("now time is.." + date_start);

Thanks..

Upvotes: 1

Related Questions