有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从以下类型格式化日期

我有日期类型的列表。我的要求是在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) 个答案

  1. # 1 楼答案

    实际上,要从这些字符串中获取以毫秒为单位的时间,您可以:

    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))您将得到一个字符串,显示基于当前系统时间的相对时间

  2. # 2 楼答案

    这对我很有效,伙计们!!你可以重复使用这个。。。直接复制粘贴就行了

    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) );
    }
    
    }