Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-13-2007, 06:05 PM
Member
 
Join Date: Dec 2007
Location: Massachusetts, USA
Posts: 3
amyedwards is on a distinguished road
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)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-14-2007, 04:41 AM
Member
 
Join Date: Nov 2007
Posts: 11
rajiv_bang is on a distinguished road
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?

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; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-14-2007, 09:46 PM
Member
 
Join Date: Dec 2007
Location: Massachusetts, USA
Posts: 3
amyedwards is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-15-2007, 12:33 AM
Member
 
Join Date: Dec 2007
Location: Massachusetts, USA
Posts: 3
amyedwards is on a distinguished road
Rajiv:
I figured out what I was doing wrong and my conversion works now. Thank you SOOO much!!
Amy.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting object to string Preethi New To Java 4 06-14-2008 05:29 AM
Difference between current date and anothe date vijay balusamy New To Java 1 04-16-2008 06:15 PM
Converting String to Double srini New To Java 1 12-24-2007 10:03 PM
String to Date conversion javaplus New To Java 2 11-06-2007 09:16 PM
Date as String to TimeStamp ironballs Advanced Java 1 08-01-2007 03:43 PM


All times are GMT +3. The time now is 03:29 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org