How to get the return time in Android from the USA

package com.example.currenttime; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Functions { private static long converTimeStringINToMillis1(String time) { long milliseconds = 0; try { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // 25/06/2014 8:41:26 Date date; date = sdf.parse(time); milliseconds = date.getTime(); } catch (ParseException e) { // TODO Auto-generated catch block milliseconds = 0; e.printStackTrace(); } return milliseconds; } public static String setLastSeenTime1(String time) { long milliseconds = Math.abs(System.currentTimeMillis() - converTimeStringINToMillis1(time)); String lastSeen = ""; int seconds = (int) milliseconds / 1000; if (seconds < 60) lastSeen = String.valueOf(seconds) + "sec ago"; else if (seconds > 60 && seconds < 3600) lastSeen = String.valueOf((int) seconds / 60) + " min ago"; else if (seconds > 3600 && seconds < 86400) lastSeen = String.valueOf((int) seconds / 3600) + " hours ago"; else if (seconds > 86400 && seconds < 172800) lastSeen = " Yesterday"; else if (seconds > 172800 && seconds < 2592000) lastSeen = String.valueOf((int) (seconds / (24 * 3600))) + " days ago"; else if (seconds > 2592000) lastSeen = String.valueOf((int) (seconds / (30 * 24 * 3600))) + " months ago"; return lastSeen; } } 

I have the inverse display time, I call this function. Using this Android code:

 btn_retry.setText(Functions.setLastSeenTime1("09/10/2014 8:41:26 AM")) 

But the problem is that I get the server time, which is in Us, I have to display according to the local ZOne time, so that I can see that you have chosen the time for reviews. Please suggest me how I will do where I do wrong, please help.

0
source share
3 answers

Update: This should work, just replace it in your utility class, you just need to pass the time and the TimeZone server.

 public static String setLastSeenTime1(String time, TimeZone serverTimeZone) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); sdf.setTimeZone(serverTimeZone); SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); sdf.setTimeZone(TimeZone.getDefault()); return sdf2.format(sdf.parse(time)); } 
+1
source

in convertTimeStringINToMillis1 use like this

  Date date = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Use UTC time zone to format the date in df.setTimeZone(TimeZone.getTimeZone("UTC")); System.out.println("Date and time in UTC: " + df.format(date)); 
+1
source

Joda time

Instead of rolling the roll yourself, use the Joda-Time library. Joda-Time runs on Android.

The code example below uses Joda-Time 2.4.

Joda-Time uses ISO 8601 standard line formats as the default values ​​for parsing and generating line output. Here you can see the lines DateTime , Interval and Period.

Parse the input string. Notice how we specify the time zone and Locale (to analyze the value of "AM" in English).

 String input = "09/10/2014 8:41:26 AM"; DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "MM/dd/yyyy hh:mm:ss a" ).withZoneUTC().withLocale( Locale.ENGLISH ); DateTime then = formatterInput.parseDateTime( input ); 

Get the current moment. Use this moment to build Interval from now on.

 DateTime now = DateTime.now( DateTimeZone.UTC ); Interval interval = new Interval( then , now ); 

Convert interval to Period , time period defined as the number of days, hours, etc.

 Period period = interval.toPeriod(); 

Create a text view of the Period. This example refers to the default period formatting. Instead, you can create your own formatter using the PeriodFormatterBuilder class.

 PeriodFormatter formatterOutput = PeriodFormat.wordBased( Locale.US ); String output = formatterOutput.print( period ); 

For fun, show a different language. Arbitrary choice of Quebec style.

 String outputQuébécois = PeriodFormat.wordBased( Locale.CANADA_FRENCH ).print( period ); 

Dump for the console.

 System.out.println( "input: " + input ); System.out.println( "then: " + then ); System.out.println( "now: " + now ); System.out.println( "interval: " + interval ); System.out.println( "period: " + period ); System.out.println( "output: " + output ); System.out.println( "outputQuébécois: " + outputQuébécois ); 

At startup.

 input: 09/10/2014 8:41:26 AM then: 2014-09-10T08:41:26.000Z now: 2014-10-09T20:44:23.470Z interval: 2014-09-10T08:41:26.000Z/2014-10-09T20:44:23.470Z period: P4W1DT12H2M57.470S output: 4 weeks, 1 day, 12 hours, 2 minutes, 57 seconds and 470 milliseconds outputQuébécois: 4 semaines, 1 jour, 12 heures, 2 minutes, 57 secondes et 470 millisecondes 
0
source

Source: https://habr.com/ru/post/1204393/


All Articles