如何获得当地时间的Android

时间:2014-10-30 06:05:49

标签: android datetime datetimeoffset

我的应用程序中有一个日期和时间的字符串,例如: - 2014-10-30T11:30:00我想将此String转换为我当地的时间,这应该是这样的  2014-10-30T11:30:00+05:30。我不能操作字符串,因为服务器端转换是为了进行时间的加法和减法。如何将+05:30偏移量添加到我的时间?

如何使用本地时间进行操作,以便服务器知道我的语言环境?

2 个答案:

答案 0 :(得分:0)

  Calendar cal = Calendar.getInstance();
  TimeZone tz = cal.getTimeZone();
  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
  sdf.setTimeZone(tz);
  long timestamp = cursor.getLong(columnIndex);
  Log.d("Server time: ", timestamp);
  String localTime = sdf.format(new Date(timestamp * 1000));
  Log.d("Time: ", localTime);

答案 1 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

如何使用给定的功能:

convertDateStringFormat( “2014-10-30T11:30:00”, “YYYY-MM-dd'T'hh:MM:SS”, “YYYY-MM-dd'T'hh:MM:SS”)< / p>

这里你传递了三个参数:

1.strDate    : which is represent datetime string.
2.fromFormat : which is represent datetime format of your given datetime string.
3.toFormat   : which is represent datetime format which you wan to convert your given  datetime string. 

public String convertDateStringFormat(String strDate, String fromFormat, String toFormat){
   try{
       SimpleDateFormat sdf = new SimpleDateFormat(fromFormat);
       sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
       SimpleDateFormat dateFormat2 = new SimpleDateFormat(toFormat.trim());
       return dateFormat2.format(sdf.parse(strDate));
    }catch (Exception e) {
       e.printStackTrace();
       return "";
    }  
}