在android中实现漂亮的时间格式化日期时间

在android中实现漂亮的时间格式化日期时间,android,Android,我正在这个网站上实现一个漂亮的时间库,以便在我的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();

我正在这个网站上实现一个漂亮的时间库,以便在我的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
...

如果有什么我需要做的,如果有人能帮忙,我将不胜感激。谢谢

我不确定什么是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";
    }
}
private static final int SECOND\u MILLIS=1000;
专用静态最终整数分钟=60*秒;
专用静态最终整数小时=60*分钟;
私人静态最终整数天=24*小时;
公共静态日期currentDate(){
日历=Calendar.getInstance();
返回calendar.getTime();
}
公共静态字符串getTimeAgo(长时间){
如果(时间<10000000000L){
//如果时间戳以秒为单位,则转换为毫秒
时间*=1000;
}
long now=currentDate().getTime();

如果(time>now | | time您可以创建自己的函数来完成此操作。请尝试此示例

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

实际上,您没有将日期传递到
PrettyTime
对象,因为日期转换失败。请在日志猫中确认

问题是您正在将不同的日期格式和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");

希望这将消除所有疑问

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

嘿,我没有否决你@323go,谢谢你告诉我。这对我来说很有效,但我不知道为什么有人会否决你,因为我很高兴能帮上忙。同时,谢谢你让我意识到了美好时光——这比我自己滚要容易得多。请你解释一下为什么你否决了。