开发者

how to convert from 10/21/2009 to Wed Oct 21 18:48:12 UTC+0530 2009

hi can any one please help me on how to con开发者_如何学Govert from 10/21/2009 to Wed Oct 21 18:48:12 UTC+0530 2009 in my java class


Quite frankly, you can't convert 10/21/2009 to Wed Oct 21 18:48:12 UTC+0530 for the simple reason that the first date does contain neither time nor timezone information.


String dateString = new SimpleDateFormat("dd/MM/yyyy").parse(d).toString();

Here you'll get more information: https://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html.


You don't specify if the original value (10/21/2009) is a date object or a string, I'm going to assume that it is in fact a String, so you first need to parse the string and convert it to a date object using the SimpleDateFormatter class

import java.text.SimpleDateFormatter;

SimpleDateFormatter sdf = new SimpleDateFormatter("MM/dd/yyyy");
Date d = sdf.parse("10/21/2009");

Once you have a date object, you can just call toString() on it which will get you the string you want, or create another SimpleDateFormatter object with the pattern that you want (check the javadocs) and then call format().

There's a couple of things you should be aware of regarding the DateFormatter classes available in the JDK

  • If the string can not be parsed, the parse method will throw an exception, you will need to handle the exception accordingly
  • The date formatter classes available in the JDK are not threadsafe. Don't create a private/protected/public member variable of these types, instead create the formatters/parsers in the methods where you need them
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜