Cannot get date from MS

11 views
Skip to first unread message

Gareth Murfin

unread,
Mar 12, 2018, 2:18:07 AM3/12/18
to CodenameOne Discussions
I have all sorts of methods ive made over the years for making date strings, but they dont seem to work now. Everytime I pass in ms they come up with 1970. 

Example:  1520764749 on this website (https://www.freeformatter.com/epoch-timestamp-to-date-converter.html) shows 3/11/2018, 6:39:37 PM

but if I pass it into any of my methods I always get 18/01/1970. Am I doing somethign wrong or has there somehow been a new bug introduced into the Data class?

    public static String getDateString(Date d)
    {
        if (d==null)
        {
            _("*warning* date passed in was null, we will assume that this is 'now' and make a new one");
            d= new Date();
        }
        //formulate this date objects
        SimpleDateFormat df = new SimpleDateFormat();
        //we want: 2016-11-21T15:47:21+00:00
       // String dateString = df.format("yyyy-MM-dd hh:mm:ss a", d);
       df.applyPattern("yyyy-MM-dd hh:mm:ss a");//PROBABLY WRONG AFTER CN1 CONVERSION
        String dateString = df.format( d);
   ///     _("dateString is ->"+dateString);
        return dateString.toString();
    }
      //takes a date object in and returns a string like 19/09/2011
    public static String getDateString_(Date d)
    {
        Calendar calendar = null;
        calendar = Calendar.getInstance();        
        calendar.setTime(d);        
        String day    = calendar.get(Calendar.DAY_OF_MONTH)+"";
        String month  = (calendar.get(Calendar.MONTH)+1)+""; //month starts at 0 in java
        if (month.length()==1)
        {
            month="0"+month; // getthe zero before or it looks odd
        }
        String year   = calendar.get(Calendar.YEAR)+"";        
        return day+"/"+month+"/"+year;
    }
    
     public static String getDateString__(Date date)
      {        
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year    = cal.get(Calendar.YEAR);
        int month   = cal.get(Calendar.MONTH);
        int day     = cal.get(Calendar.DAY_OF_MONTH);
        
        String writeme = day+"/"+month+"/"+year;
        //old way was this, so we cna test 
        //REMOVE THIS, ONLY PUT IT BACK IN FOR TESTING BACKWARDS
        //OLD WAY NOT IN USE ANYMORE writeme = day+"-"+month+"-"+year;
        
        _("getDateString__ returning "+writeme);
        return writeme;
      }
    
     
        //takes a date object in and returns a string like 19/09/2011
    public static String getDateString___(Date d)
    {
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        
        calendar.setTime(d);
        
        String day    = calendar.get(Calendar.DAY_OF_MONTH)+"";
        String month  = (calendar.get(Calendar.MONTH)+1)+""; //month starts at 0 in java
        if (month.length()==1)
        {
            month="0"+month; // getthe zero before or it looks odd
        }
        String year   = calendar.get(Calendar.YEAR)+"";
        
        return day+"/"+month+"/"+year;
    }

Gareth Murfin

unread,
Mar 12, 2018, 2:23:03 AM3/12/18
to CodenameOne Discussions
I noticed that the ms from System.currentMillis has 3 extra digits, ie 

1520835640781   from system.currentimemillis
1520764777      from api im using

so I assume I just need to add some value to the one that is giving me errors? Think ive done this before but i cannot recall what I need to add.

Shai Almog

unread,
Mar 13, 2018, 1:03:04 AM3/13/18
to CodenameOne Discussions
You are getting seconds not milliseconds.

Gareth Murfin

unread,
Mar 13, 2018, 3:09:20 PM3/13/18
to CodenameOne Discussions
Doh!!! what an idiot I am, man... Thanks Shai!!
Reply all
Reply to author
Forward
0 new messages