Results 1 to 4 of 4
  1. #1
    amyedwards is offline Member
    Join Date
    Dec 2007
    Location
    Massachusetts, USA
    Posts
    3
    Rep Power
    0

    Unhappy 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)

  2. #2
    rajiv_bang is offline Member
    Join Date
    Nov 2007
    Posts
    11
    Rep Power
    0

    Default

    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;
    
    }
    }

  3. #3
    amyedwards is offline Member
    Join Date
    Dec 2007
    Location
    Massachusetts, USA
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    amyedwards is offline Member
    Join Date
    Dec 2007
    Location
    Massachusetts, USA
    Posts
    3
    Rep Power
    0

    Default

    Rajiv:
    I figured out what I was doing wrong and my conversion works now. Thank you SOOO much!!
    Amy.

Similar Threads

  1. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:29 AM
  2. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 04:15 PM
  3. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 08:03 PM
  4. String to Date conversion
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 07:16 PM
  5. Date as String to TimeStamp
    By ironballs in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 01:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •