datetime - How to convert different date formats into timestamp object in java? -


i have following code convert different string representations of date/datetime timestamp object:

public class generalmethods { public static string[] formats = {"dd/mm/yyyy","mm/dd/yyyy hh:mm:ss","e mmm dd hh:mm:ss zzz yyyy"}; /** * method return timestamp object string date in formats * {"dd/mm/yyyy hh:mm:ss","e mmm dd hh:mm:ss zzz yyyy","dd/mm/yyyy"} * @param date: date format in string representation * @return timestamp object */ public static timestamp timestampformat(string date) { date dateobj = new date(); for(string format: formats) { try { dateobj = new simpledateformat(format).parse(date); system.out.println(dateobj.tostring()); // tracking }catch (parseexception e) { } } return new timestamp(dateobj.gettime()); } // testing public static void main (string args[]) { system.out.println(timestampformat("05/31/2011 21:37:35")); } } 

the output code is:

wed jan 05 00:31:00 gmt 2011 mon jan 31 21:37:35 gmt 2011 2011-01-31 21:37:35.0 

as can see month in output wrong input 05/31/2011 21:37:35. how happening?

mm minutes mm months patterns set minutes , months set default january.

try changing patterns "dd/mm/yyyy", "mm/dd/yyyy hh:mm:ss"


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -