如何格式化以下类型的日期

时间:2017-04-27 16:26:50

标签: java android

我有日期类型列表。我的要求是在ArrayList<String>中按升序格式化日期,并需要一种实用工具方法来获取“1天前”,“2天前”等字符串。 “40天前”就是这样。从以下测试数据。

2017-03-31T19:56:06.733Z
2017-03-31T19:55:38.227Z
2017-04-25T18:01:26.069Z
2017-04-25T17:57:49.656Z
2017-04-25T17:59:18.867Z

2 个答案:

答案 0 :(得分:0)

对我来说这对你有用!!您可以重复使用此...直接复制粘贴将起作用..

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TesterClass {



public static int getDaysLeft(String date) {


    final Date systemDate = new Date();
    final Calendar couponDateCalenderInstance = Calendar.getInstance();
    final Calendar systemDateCalenderInstance = Calendar.getInstance();


    int days;
    try {
             Date tempDate;

            final DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
            tempDate = dateformat.parse(date);
            couponDateCalenderInstance.setTime(tempDate);
            systemDateCalenderInstance.setTime(systemDate);
            final long diff = couponDateCalenderInstance.getTimeInMillis() -
                    systemDateCalenderInstance.getTimeInMillis();
            days = (int) (diff / (24 * 60 * 60 * 1000));
    } catch (Exception e) {
        days = -1;
        System.out.println(e);
    }
    return days;
}

public static void main(String[] args) {

       String testData =  "2017-03-31T19:56:06.733Z";

       String parsedString = testData.substring(0,10);
       System.out.println(parsedString);

       System.out.println("Days Left " + TesterClass.getDaysLeft(parsedString) );
}

}

答案 1 :(得分:0)

实际上,要从那些字符串中获得毫秒的时间,您可以:

public static long getTimeInMillis(String rawDate){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.SSS");
        try {
            Date dateTime = dateFormat.parse(rawDate);
            long millis = dateTime.getTime();
            return millis;
        } catch (Exception e){
            e.printStackTrace();
            Log.e("DateParser", e.getMessage(), e);
            return 0;
        }
    }

要将其转换为相对时间,您可以:

public static String getTimeDifference(long time) {
        if (time < 1000000000000L) {
            // if timestamp given in seconds, convert to millis
            time *= 1000;
        }

        long now = System.currentTimeMillis();
        if (time > now || time <= 0) {
            return null;
        }

        // TODO: localize
        final long diff = now - time;
        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 "an 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";
        }
    }

因此,通过调用 getTimeDifference(getTimeInMillis(rawDate)),您将根据当前系统时间来显示相对时间的字符串.. < / p>