0

I am calling current weather API response from https://openweathermap.org/current

The API response has sunset and sunrise value like this : "sunrise": 1560343627, "sunset": 1560396563 The parameter unit is written as Sunset time, Unix, UTC

I know the location and now I want to convert this time to the local time of the place. How can I do it?

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
Sneha
  • 94
  • 10
  • Does this answer your question? [How to convert UTC timestamp to device local time in android](https://stackoverflow.com/questions/14853389/how-to-convert-utc-timestamp-to-device-local-time-in-android) – Sayok Majumder May 05 '20 at 04:55

3 Answers3

3

Use java.time classes.

int millisSinceEpoch = Integer.parseInt( "1560343627" ) ;
Instant instant = Instant.ofEpochMilli( millisSinceEpoch ) ;

That Instant object represents a moment as seen in UTC. You can adjust to a time zone to see that same moment through the wall-clock time used by the people of a certain region.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

For converting timestamp to current time

Calendar calendar = Calendar.getInstance();
TimeZone tz = TimeZone.getDefault();
calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
java.util.Date currenTimeZone=new java.util.Date((long)1379487711*1000);
Toast.makeText(TimeStampChkActivity.this, sdf.format(currenTimeZone), Toast.LENGTH_SHORT).show();

Hope it will work

Thankew! Happy coding!

zinonX
  • 330
  • 2
  • 20
  • 1
    These terrible date-time classes are now legacy, supplanted years ago by the modern *java.time* classes defined in JSR 310. – Basil Bourque May 05 '20 at 06:48
0
public static String covertUnixToHour(int sunrise){
    Date date = new Date(sunrise* 1000L);
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    String formatted = sdf.format(date);
    return formatted;
}
  • 4
    While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – fcdt Sep 30 '20 at 10:59