Skip to content

Instantly share code, notes, and snippets.

@anehkumar
Created December 27, 2015 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anehkumar/a95421f5bbecb570de8f to your computer and use it in GitHub Desktop.
Save anehkumar/a95421f5bbecb570de8f to your computer and use it in GitHub Desktop.
Calendar cal = Calendar.getInstance();
long milliDiff = cal.get(Calendar.ZONE_OFFSET);
// Getting time Zone
String[] ids = TimeZone.getAvailableIDs();
String name = null;
for (String id : ids) {
TimeZone tz = TimeZone.getTimeZone(id);
if (tz.getRawOffset() == milliDiff) {
// Found a match.
name = id;
break;
}
}
System.out.println(name);
//current date time According to GMT 00
Calendar call = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentLocalTime = call.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
String localTime = date.format(currentLocalTime);
System.out.println(localTime);
//current date time according to time zone
TimeZone tz = TimeZone.getTimeZone(name);
SimpleDateFormat destFormat = new SimpleDateFormat("dd-MM-yyy HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(currentLocalTime);
System.out.println(result);
DateFormat dfm = new SimpleDateFormat("dd-MM-yyy HH:mm:ss");
try {
Date unixtime = dfm.parse(result);
Date NowDate = dfm.parse(localTime);
String timePassedString = String.valueOf(getRelativeTimeSpanString(NowDate.getTime(), unixtime.getTime(), DateUtils.MINUTE_IN_MILLIS));
System.out.println(timePassedString);
} catch (ParseException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment