TimeStamp to String Android

时间:2017-01-11 11:13:55

标签: android string timestamp timestamp-with-timezone

服务器给了我这种格式的时间:

 Tue Jan 10 03:48:45 GMT+05:30 2017"

我想将其转换为:

Tue,Jan 2017"

我已经搜索谷歌一段时间了,仍然没有运气。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

以下代码可帮助您将一种时间格式转换为另一种格式:

public static String formatDateFromOnetoAnother(String date) {
    String resultformat = "EEE,MMM dd";
    String givenformat = "EEE MMM dd zZ yyyy";
    String result = "";
    SimpleDateFormat sdf;
    SimpleDateFormat sdf1;
    try {
        sdf = new SimpleDateFormat(givenformat);
        sdf1 = new SimpleDateFormat(resultformat);
        result = sdf1.format(sdf.parse(date));
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    } finally {
        sdf = null;
        sdf1 = null;
    }

    return result;
}