如何将日期时间转换为以前的格式2019-02-28T11:30:00.000Z?

时间:2019-02-28 15:27:14

标签: java android

我有一个类似2019-02-28T11:30:00.000Z的日期,我想将其转换成以前的格式,或者在我使用它之前的格式,但是它显示以前的格式不正确,请解决此问题。

public class CustomDateFormat {
public static String DateToTimeFormat(String oldstringDate) {
    PrettyTime p = new PrettyTime();
    String isTime = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",new Locale(getCountry()));
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = sdf.parse(oldstringDate);
        isTime = p.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return isTime;
}

public static String DateFormat(String oldstringDate) {
    String newDate;
    SimpleDateFormat dateFormat = new SimpleDateFormat("E, d MMM yyyy", 
new Locale(getCountry()));
    try {
        Date date = new SimpleDateFormat("yyyy-MM-dd", new Locale(getCountry())).parse(oldstringDate);
        newDate = dateFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
        newDate = oldstringDate;
    }
    return newDate;
}

public static String getCountry() {
    Locale locale = Locale.getDefault();
    String country = String.valueOf(locale.getCountry());
    return country.toLowerCase();
  }
}

1 个答案:

答案 0 :(得分:0)

我有一些东西可以找出“ ago”。同样,您可以尝试使用“之前”

public String gettimeago(String ntime) {



        final int SECOND_MILLIS = 1000;

        final int MINUTE_MILLIS = 60 * SECOND_MILLIS;

        final int HOUR_MILLIS = 60 * MINUTE_MILLIS;

        final int DAY_MILLIS = 24 * HOUR_MILLIS;


        if (ntime != null) {

            long earlier = Long.parseLong(ntime);


            long now = System.currentTimeMillis();

            if (earlier < 1000000000000L) {

                earlier *= 1000;

            }


            if (earlier > now || earlier <= 0) {

                return null;

            }


            final long diff = now - earlier;

            if (diff < MINUTE_MILLIS) {

                return "just now";

            } else if (diff < 2 * MINUTE_MILLIS) {

                return "a minute ago";

            } else if (diff < 50 * MINUTE_MILLIS) {

                return (diff / MINUTE_MILLIS + " minutes ago");

            } else if (diff < 90 * MINUTE_MILLIS) {

                return "a hour ago";

            } else if (diff < 24 * HOUR_MILLIS) {

                return (diff / HOUR_MILLIS + " hours ago");

            } else if (diff < 48 * HOUR_MILLIS) {

                return "yesterday";

            } else {

                return diff / DAY_MILLIS + " days ago";

            }



        }


        return "";


    }