1

Possible Duplicate:
how to parse date in java?

I want to convert the string "11-10-10 12:00:00" into a Date object, but I am not able to do so. Can you please help me out?

I have the Date object which has the value "Mon Oct 11 00:00:00 IST 2010"

DateFormat newDateFormat = new SimpleDateFormat("dd-MM-yy hh:mm:ss");    
String strDate = newDateFormat.format(tempDate);  
//**i got strDate as strDate is : 11-10-10 12:00:00**
DateFormat newDateFormat1 = new SimpleDateFormat("dd-MM-yy hh:mm:ss");    
try {    
 tempDate = newDateFormat1.parse(strDate); 
     // **getting tempDate as - Mon Oct 11 00:00:00 IST 2010**    
   } catch (ParseException e) {    
 // TODO Auto-generated catch block    
 e.printStackTrace();    
 }
Community
  • 1
  • 1
Mahendra Athneria
  • 1,203
  • 3
  • 16
  • 32
  • What language - it looks like C#. Also this has been asked many times before. Check out the questions under "Related" -> – ChrisF Jan 03 '11 at 13:55

3 Answers3

7
DateFormat newDateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");    
Date d = newDateFormat.parse("11-10-10 12:00:00");
System.out.println(d);

Here is ideone demo

  • Hi Hilal, thanks for the reply. but your code producing the same output. d= "Mon Oct 11 12:00:00 IST 2010" and i want in "dd-MM-yy HH:mm:ss" format. – Mahendra Athneria Jan 03 '11 at 18:22
  • 1
    This code parses the date, as you asked. Printing it is a whole other issue. A Date is just a number, and the date and format are not related to each other, so Java will print the date however it pleases. – cHao Jan 03 '11 at 21:49
0

I think this is Java code—not my specialty—but I think your issue is the "hh" in your format string, which causes parse to interpret "12:00:00" as midnight instead of noon. Try changing it to "HH" and see if that parses correctly.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
0

You need to use CultureInfo for Hindi it is "hi-IN". For full list of cultures check this link CultureInfo

    class Program
{
    static void Main(string[] args)
    {
        var str = "11-10-10 12:00:00";

        DateTime dateTime = DateTime.Parse(str, new CultureInfo("hi-IN", false));
    }
}
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179