Results 1 to 4 of 4
Thread: Converting Epoch to string Date
- 12-13-2007, 04:05 PM #1
Member
- Join Date
- Dec 2007
- Location
- Massachusetts, USA
- Posts
- 3
- Rep Power
- 0
Converting Epoch to string Date
Hello all:
I'm new here and though I'm not "new" to Java per se, I don't use it on a daily basis and I find that I need a little help with a conversion.
I have an attribute on an object (LAST_MODIFIED) whose value is a date in epoch format (e.g., 1197560243, which is Dec 13, 2007). I'm doing a little conversion based on some posts I read here, to get it into a readable format. That code looks like this:
private String convertDate( Long str )
{
Date epoch = new Date(str);
DateFormat df2 = DateFormat.getDateInstance(DateFormat.MEDIUM);
String s2 = df2.format(epoch);
return s2;
}
The value of the converted date, however, always ends up being "Jan 14, 1970", which (obviously) isn't correct. Can anyone tell me where I'm going wrong? Any assistance is greatly appreciated!!
Kind regards,
Amy. (who is exceedingly frustrated)
- 12-14-2007, 02:41 AM #2
Member
- Join Date
- Nov 2007
- Posts
- 11
- Rep Power
- 0
The Date constructor accepts milliseconds. I have not made any changes to your method except for passing the input in milliseconds.
Can you try this out?
Java Code:import java.text.DateFormat; import java.util.*; public class ConvertDate { public static void main(String args[]) { ConvertDate f = new ConvertDate(); Long str = new Long(1197560243000L); System.out.println(f.convertDate(str)); } private String convertDate( Long str ) { Date epoch = new Date(str); DateFormat df2 = DateFormat.getDateInstance(DateFormat.MEDIUM); String s2 = df2.format(epoch); return s2; } }
- 12-14-2007, 07:46 PM #3
Member
- Join Date
- Dec 2007
- Location
- Massachusetts, USA
- Posts
- 3
- Rep Power
- 0
Thank you, Rajiv. I have tried adding the code that you suggested but the behavior is the same. I suspect that I'm adding it incorrectly. Should I create a separate class file for the new class you created or just include it as a subclass of mine?
Thanks again.
- 12-14-2007, 10:33 PM #4
Member
- Join Date
- Dec 2007
- Location
- Massachusetts, USA
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Converting object to string
By Preethi in forum New To JavaReplies: 4Last Post: 06-14-2008, 03:29 AM -
Difference between current date and anothe date
By vijay balusamy in forum New To JavaReplies: 1Last Post: 04-16-2008, 04:15 PM -
Converting String to Double
By srini in forum New To JavaReplies: 1Last Post: 12-24-2007, 08:03 PM -
String to Date conversion
By javaplus in forum New To JavaReplies: 2Last Post: 11-06-2007, 07:16 PM -
Date as String to TimeStamp
By ironballs in forum Advanced JavaReplies: 1Last Post: 08-01-2007, 01:43 PM
Bookmarks