Reputation:
I have write code to get time from long and get long from time. While I am running I can't get proper datetime from long. Can you help me to fix this issue.
TimeConverterUtil.java
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeConverterUtil
{
public static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static SimpleDateFormat usrFormatter = new SimpleDateFormat("dd MMM yy, HH:mm:ss:ms");
public static String getDateTime(Long stamp)
{
Date date = new Date(stamp);
return formatter.format(date);
}
public static Long getTimestamp(String date)
{
Date lFromDate1;
try {
lFromDate1 = usrFormatter.parse(date);
return lFromDate1.getTime();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
Testing Program:
TCTester.java
//$Id$
public class TCTester {
/**
* @param args
*/
public static void main(String[] args)
{
Long timeInLong = TimeConverterUtil.getTimestamp("28 Mar 13, 02:14:02:000");
System.out.println(TimeConverterUtil.getDateTime(timeInLong));
System.out.println("\n\n\n"+TimeConverterUtil.getTimestamp("28 Mar 13, 02:14:02:000"));
}
}
Upvotes: 1
Views: 92
Reputation: 1499900
The problem is in the milliseconds part of your input format string. You've got a format of:
"dd MMM yy, HH:mm:ss:ms"
but ms
is being interpreted as "minutes then seconds", not "milliseconds". You want:
"dd MMM yy, HH:mm:ss:SSS"
Note that you're not including milliseconds in your output format string, so that part of the information will be lost anyway.
Also note that it's odd to use ":" between seconds and milliseconds. It would be more normal to use ".", so the input might be "28 Mar 13, 02:14:02.000"
.
Upvotes: 3