0

I am trying to make an app where my app sending info with local time to the server, as my app will be used in a different country so I want to show the info send time with respect to local time. for example, if I send an info with local timeaug 19,2017 11.37 am from Bangladesh time to server, then the info will show in south America with the time aug 19,2017 0.36 am. my server is in America/Chicago.

I am trying with below code but not working:

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/Chicago"));

Date newDate= null;

try {
   newDate = simpleDateFormat.parse("2017-08-19 11:15:21");
   simpleDateFormat.setTimeZone(TimeZone.getDefault());

   String desireDate=simpleDateFormat.format(newDate);
   Log.v("TimeStamp",desireDate.toString());
} catch (ParseException e) {
   e.printStackTrace();
}

I saw lots of similar problem in SO but no one working for me.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
AAA
  • 485
  • 6
  • 23
  • I think you should consider storing everything on your server in UTC time. Then, just convert locally to the specific timezone of the app. – Tim Biegeleisen Aug 19 '17 at 05:43
  • Check this answer [getting-simpledateformat-for-specific-timezone](https://stackoverflow.com/questions/16964910/getting-simpledateformat-for-specific-timezone) – ELITE Aug 19 '17 at 05:52
  • Also this answer is relevant [how-set-timezone-in-android](https://stackoverflow.com/questions/22814263/how-set-timezone-in-android) – ELITE Aug 19 '17 at 05:53

3 Answers3

0

Use this code , Its working for me .. check here for timezones ,don't forget to use this import android.icu.text.SimpleDateFormat for >24 , <24 use this import java.text.SimpleDateFormat;

DateFormat yourformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone utc = TimeZone.getTimeZone("America/Chicago");
GregorianCalendar gc = new GregorianCalendar(utc);
Date now = gc.getTime();
Toast.makeText(this, yourformat.format(now) , Toast.LENGTH_SHORT).show();
Omar Dhanish
  • 885
  • 7
  • 18
0

Just for an example.

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
// you can get seconds by adding  "...:ss" to it
date.setTimeZone(TimeZone.getTimeZone("GMT+1:00")); 

String localTime = date.format(currentLocalTime); 
Galib Imtiaz
  • 106
  • 2
  • 7
  • bhaia, your code giving me the exact time of chicago against local time.can you please tell me how to make output the way i want? – AAA Aug 19 '17 at 09:11
  • means how can i get local time against chicago time? – AAA Aug 19 '17 at 09:13
0

would check that the timestamp coming from your server is what you expect. If the timestamp from the server does not convert into the date you expect (in the local timezone) then the difference between the timestamp and the current system time will not be what you expect.

Use Calendar to get the current timezone. Initialize a SimpleDateFormatter with the current timezone; then log the server timestamp and verify if it's the date you expect:

Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();

/* debug: is it local time? */
Log.d("Time zone: ", tz.getDisplayName());

/* date formatter in local timezone */
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
sdf.setTimeZone(tz);

/* print your timestamp and double check it's the date you expect */
long timestamp = cursor.getLong(columnIndex);
String localTime = sdf.format(new Date(timestamp * 1000)); // I assume your timestamp is in seconds and you're converting to milliseconds?
Log.d("Time: ", localTime);

If the server time that is printed is not what you expect then your server time is not in UTC.

If the server time that is printed is the date that you expect then you should not have to apply the rawoffset to it. So your code would be simpler (minus all the debug logging):

long timestamp = cursor.getLong(columnIndex);
Log.d("Server time: ", timestamp);

/* log the device timezone */
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
Log.d("Time zone: ", tz.getDisplayName());

/* log the system time */
Log.d("System time: ", System.currentTimeMillis());

CharSequence relTime = DateUtils.getRelativeTimeSpanString(
    timestamp * 1000,
    System.currentTimeMillis(),
    DateUtils.MINUTE_IN_MILLIS);

((TextView) view).setText(relTime);