在android中实现格式化日期时间的美好时光

时间:2015-09-30 02:54:19

标签: android

我正在从这个网站http://www.ocpsoft.org/prettytime/实现漂亮时间库,以便在我的Android应用程序中获取带有文本的格式化日期字符串。

我将我的日期作为字符串从我的本地sqlite数据库中提供,

     String dateString="2015-09-25 15:00:47";
     SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");

     Date convertedDate = new Date();

     try {
         convertedDate = dateFormat.parse(dateString);
     } catch (ParseException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }

     PrettyTime p  = new PrettyTime();

     String datetime= p.format(convertedDate);
     textview.setText(datetime);

我希望能够使用parsetext格式化我的日期

 2 minutes ago
 1 day ago
 ...

然而,上面的代码段代码只提供了文字并避免了时间。 这就是它提供的内容

 minutes ago
...

请有什么我需要做的,如果有人可以提供帮助,我将不胜感激。感谢

4 个答案:

答案 0 :(得分:4)

您实际上并没有将日期传递到PrettyTime对象,因为您的日期转换失败了。在logcat中确认。

问题在于您将不同的日期格式和24小时时间传递给此日期格式:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");

您可以更改日期格式字符串以使用24小时时间并匹配您的日期格式,如下所示:

String dateString="2015-09-25 15:00:47";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

或将您传入的时间更改为:

 String dateString="09/25/2015-09-25 3:00:47 pm";
 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");

答案 1 :(得分:1)

我不确定PrettyTime是什么,但你可以使用这样的东西:

private static final int SECOND_MILLIS = 1000;
private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
private static final int DAY_MILLIS = 24 * HOUR_MILLIS;

public static Date currentDate() {
    Calendar calendar = Calendar.getInstance();
    return calendar.getTime();
}

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

    long now = currentDate().getTime();
    if (time > now || time <= 0) {
        return "in the future";
    }

    final long diff = now - time;
    if (diff < MINUTE_MILLIS) {
        return "Moments ago";
    } 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";
    }
}

答案 2 :(得分:0)

你可以创建自己的功能来做到这一点。试试这个样本

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Calendar RecordTime = Calendar.getInstance();
            try
            {
                RecordTime.setTime(formatter.parse("2015-09-25 15:00:47"));
            } catch (ParseException e)
            {
                Log.e("EXP",e.toString());
                e.printStackTrace();
            }
            Calendar timeNow = Calendar.getInstance();
            long difference = timeNow.getTimeInMillis() - RecordTime.getTimeInMillis();
            long minutesago = difference /(1000*60);
            Log.e("Difference", difference + "");
            txtSyncTime.setText(minutesago + " Minutes Ago...");

您还可以将代码中的简单日期格式更改为

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

答案 3 :(得分:0)

希望这将清除所有疑问。

Date inputDate;
Date outputDate;
String formattedDateString;
String prettyTimeString;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView= (TextView) findViewById(R.id.textViewDisplay);
    String dateString="Sat Mar 18 01:29:01 EDT 2017";
    SimpleDateFormat convertToDate=new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
    SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
    try {
        inputDate=convertToDate.parse(dateString);
        formattedDateString=newDateFormat.format(inputDate);
        outputDate=newDateFormat.parse(formattedDateString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    PrettyTime prettyTime=new PrettyTime();
    prettyTimeString=prettyTime.format(outputDate);
    textView.setText(prettyTimeString);
}