2

Hi I have an android app using webRequestreturn timestamp of type Int (or Long) I want to convert it to human reader date time (according to devices timezone)

for example 1175714200 convert to GMT: Wed, 04 Apr 2007 19:16:40 GMT Your time zone: 4/5/2007, 3:16:40 AM GMT+8:00

I have use this function to convert but seem not return correct result (all result is like (15/01/1970 04:04:25) which is not correct

time.setText(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").
                format(new Date(topStory.getTime() * 1000)));

Anything wrong with the above code?

I also have the warning message:

To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates. less... (Ctrl+F1) Almost all callers should use getDateInstance(), getDateTimeInstance(), or getTimeInstance() to get a ready-made instance of SimpleDateFormat suitable for the user's locale. The main reason you'd create an instance this class directly is because you need to format/parse a specific machine-readable format, in which case you almost certainly want to explicitly ask for US to ensure that you get ASCII digits (rather than, say, Arabic digits).

Lê Khánh Vinh
  • 2,591
  • 5
  • 31
  • 77
  • You might want to try these: http://stackoverflow.com/questions/17432735/convert-unix-time-stamp-to-date-in-java and http://stackoverflow.com/questions/8034956/unix-timestamp-to-datetime-in-android Hope it helps! – Jonathan Darryl Apr 25 '16 at 04:40
  • Possible duplicate of http://stackoverflow.com/questions/4375982/converting-timestamp-as-string-to-date-in-android – Dhruv Apr 25 '16 at 05:24
  • Does `topStory.getTime()` return an int or a long? In the first case you might get a number range overflow. – Henry Apr 25 '16 at 05:35
  • hi i get the value from this url: https://hacker-news.firebaseio.com/v0/item/8863.json?print=pretty. think it's an int – Lê Khánh Vinh Apr 25 '16 at 05:37

5 Answers5

3

Try this function:

private String formatDate(long milliseconds) /* This is your topStory.getTime()*1000 */ {
    DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy' 'HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliseconds);
    TimeZone tz = TimeZone.getDefault();
    sdf.setTimeZone(tz);
    return sdf.format(calendar.getTime());
}

It gets the default timezone from the device it's being used on. If you have any questions/doubts, please comment. Also, this function takes as input the number of milliseconds since the last epoch. If that's not what your topStory.getTime() is returning, this function will not work. In that case, you would need to convert the return value of topStory.getTime() to the number of milliseconds since the last epoch.

Ishita Sinha
  • 2,168
  • 4
  • 25
  • 38
  • Hi thanks for your help. the func taking String as prameter. but the input is int (or long) can this still be use? – Lê Khánh Vinh Apr 25 '16 at 04:13
  • I try use your function with modify parameter to int. but still give wrong result 18/01/1970 05:23:15 private String formatDate(int timestamp) { ...} – Lê Khánh Vinh Apr 25 '16 at 04:23
  • The input to the function `stringDate` should be your `topStory.getTime() * 1000`. So sorry for missing that out. Please check my edit. Also, this function takes as input the number of milliseconds since the last epoch. If that's not what your `topStory.getTime()` is returning, this function will not work. In that case, you would need to convert the return value of `topStory.getTime()` to the number of milliseconds since the last epoch. – Ishita Sinha Apr 25 '16 at 05:17
  • hi i try your func but still get wrong result like 18/01/1970. this is the url i get the time value from: https://hacker-news.firebaseio.com/v0/item/8863.json?print=pretty – Lê Khánh Vinh Apr 25 '16 at 05:39
  • I tried topStory.getTime() * 1000L but get all result similar 21/04/2016 08:08:44 – Lê Khánh Vinh Apr 25 '16 at 05:42
  • Hi, I checked the timestamp returned by the link you posted, and it is indeed somewhere in 1970 if we take the raw value returned. I see that you have accepted my answer. Did you find a way to convert that timestamp to UTC? – Ishita Sinha Apr 25 '16 at 06:52
  • hi the time shoud be multiply by 1000 (to get miliseconds) and force to be type of long by using (topStory.getTime() * 1000L) – Lê Khánh Vinh Apr 25 '16 at 06:55
1

I use this SimpleDateFormat all the time so here is my code that I use

public static String dateToString(Date date, String format) {
    
    SimpleDateFormat formatter = new SimpleDateFormat(format);  
    return formatter.format(date);      
    
}

And I just call this from my context like

dateToString(new Date(), "dd_MM_yyyy_HH_mm");

Be careful with slashes / or \ ... Usually messing your formats with alternative meaning. new Date() makes new instance of your current time so no need for math in format()

Taken from Date | Android Developers site

Date() Initializes this Date instance to the current time.

Date(long milliseconds) Initializes this Date instance using the specified millisecond value.

Edit: If no need for current time use GregorianCalendar object!

GregorianCalendar(int year, int month, int day) Constructs a new GregorianCalendar initialized to midnight in the default TimeZone and Locale on the specified date.

Then use

GregorianCalendar cal = new GregorianCalendar(2001,11,25);
cal.add(GregorianCalendar.MONTH,2);
cal.get(GregorianCalendar.YEAR); //Returns 2002
cal.get(GregorianCalendar.MONTH); //Returns 1
cal.get(GregorianCalendar.DATE); //Returns 25
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Leolian
  • 190
  • 1
  • 7
  • Hi thanks for your help but the input is timestamp (int or long value) we need to convert it to String human readable date time – Lê Khánh Vinh Apr 25 '16 at 04:14
  • Date(long milliseconds) Initializes this Date instance using the specified millisecond value. Taken from Date | Android developers so no need for extra math in format() call IF your input is always valid – Leolian Apr 25 '16 at 04:18
1

If topStory.getTime() returns an int (instead of a long) the multiplication by 1000 may overflow the int number range.

To fix that force the multiplication to be calcualted using long numbers:

topStory.getTime() * 1000L
Henry
  • 42,982
  • 7
  • 68
  • 84
0

Is topStory a calendar instance in this code? What is the purpose of multiplying by 1000? Try replacing it with Calendar.getInstance() and remove the * 1000 to debug and check your output. If it is the current time, then it isn't the formatting that's malfunctioning, it is your input.

The warning is most likely just because your input is not an instance of one of the suggested classes(like Calendar).

Vera Gonzalez
  • 578
  • 2
  • 9
  • Hi topstory is just a model. and topStory.getTime() return the int value of timestamp for example it will return int value like 1175714200 – Lê Khánh Vinh Apr 25 '16 at 03:51
0
long DateInLong = 1584212400;
Date date = new Date(DateInLong * 1000L);
SimpleDateFormat simpledateformate = new SimpleDateFormat("yyyy-MM-dd");
String DATE = simpledateformate.format(date);
kalehmann
  • 4,821
  • 6
  • 26
  • 36