【问题标题】:how to convert a date in GMT format to another format?如何将 GMT 格式的日期转换为另一种格式?
【发布时间】:2018-06-09 17:48:15
【问题描述】:

如何将 Fri Dec 29 05:30:00 GMT 05:30 20 转换为 29-12-2017 02:28:03 android.i 中的这种格式。我已经使用下面的代码来转换日期,但我可以'不行。所以请帮帮我。

String date =  Fri Dec 29 05:30:00 GMT 05:30 20

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy h:mm:ss");

Date date1 = sdf.parse(date);

【问题讨论】:

  • GMT 格式的哪个字段是年份?

标签: android date time type-conversion


【解决方案1】:

以下两种方法可以根据您的要求转换日期

//将TimeStamp date(long)转换为String... in Date的方法

public static String makeLongTODate(long dateTime) {
    Date d = new Date(dateTime);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
    String date = simpleDateFormat.format(d);
    return date;
}

//method for converting TimeStamp date(long) to String... in date & Time
public static String makeLongTODateTime(long dateTime) {
    Date d = new Date(dateTime);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss", Locale.ENGLISH);
    String date = simpleDateFormat.format(d);
    return date;
}

【讨论】:

  • 我可以在 android 中将一个日期格式转换为另一种日期格式吗?
  • 是的,在 JSON 中检索数据时.. 您可能正在使用 Retrofit 或 Volley... JSON 以时间戳格式或长格式提供日期.. 您可以将其转换为字符串或日期格式.. .你可以使用parse方法来转换它..或者可以使用格式转换为String Date..在android中使用上面的方法来转换
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
相关资源
最近更新 更多