How To Get Reverse Time In Android From US Time
Solution 1:
Update: This should work, just replace this within your utility class, you just have to pass the time and the serverTimeZone.
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));
}
Solution 2:
in converTimeStringINToMillis1 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));
Solution 3:
Joda-Time
Rather than roll-your-own, just use the Joda-Time library. Joda-Time works in Android.
The example code below uses Joda-Time 2.4.
Joda-Time uses the ISO 8601 standard string formats as defaults for both parsing and generating string output. Seen here in the DateTime, Interval, and Period strings.
Parse the input string. Note how we specify a time zone and a Locale (to parse the "AM" value 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 that moment to build an Interval
from then to now.
DateTime now = DateTime.now( DateTimeZone.UTC );
Interval interval = new Interval( then , now );
Convert interval to a Period
, a span of time defined as a number of days, hours, and such.
Period period = interval.toPeriod();
Generate a textual representation of the Period. This example accesses a default period formatter. Instead, you may build your own custom formatter via the PeriodFormatterBuilder
class.
PeriodFormatter formatterOutput = PeriodFormat.wordBased( Locale.US );
String output = formatterOutput.print( period );
For fun, show a different language. Arbitrarily choosing Québec style.
String outputQuébécois = PeriodFormat.wordBased( Locale.CANADA_FRENCH ).print( period );
Dump to 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 );
When run.
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
Post a Comment for "How To Get Reverse Time In Android From US Time"