开发者

Convert string "11-10-10 12:00:00" into Date object [duplicate]

This question already has answers here: Closed 10 years ago.

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开发者_如何学C 11 00:00:00 IST 2010**    
   } catch (ParseException e) {    
 // TODO Auto-generated catch block    
 e.printStackTrace();    
 }


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


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.


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));
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜