我需要像今天一样打印日期,昨天,2天前,就像我已经完成的那样
我得到的日期如下:String date1 = "Thu Nov 13 19:01:25 GMT+05:30 2014";
像str=get_userTime(date1);
private String get_userTime(String usertime) {
Date d = null;
// String datee = "Thu Nov 13 19:01:25 GMT+05:30 2014";
String datee = usertime;
SimpleDateFormat inputFormat = new SimpleDateFormat(
"EE MMM dd HH:mm:ss zz yyy");
try {
d = inputFormat.parse(datee);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat outputFormat1 = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(outputFormat1.format(d));
op = outputFormat1.format(d);
op=formatToYesterdayOrToday(op);
return op;
}
这是昨天/今天我使用此Link
的另一项功能public static String formatToYesterdayOrToday(String date) {
date=op;
DateTime dateTime = DateTimeFormat.forPattern("EEE hh:mma MMM d, yyyy")
.parseDateTime(date);
DateTime today = new DateTime();
DateTime yesterday = today.minusDays(1);
DateTimeFormatter timeFormatter = DateTimeFormat.forPattern("hh:mma");
if (dateTime.toLocalDate().equals(today.toLocalDate())) {
return "Today " + timeFormatter.print(dateTime);
} else if (dateTime.toLocalDate().equals(yesterday.toLocalDate())) {
return "Yesterday " + timeFormatter.print(dateTime);
} else {
return date;
}
}
但是得到错误:
11-21 13:12:10.626: E/AndroidRuntime(20654): java.lang.IllegalArgumentException: Invalid format: "11/21/2014"
11-21 13:12:10.626: E/AndroidRuntime(20654): at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:871)
开启
DateTime dateTime = DateTimeFormat.forPattern("EEE hh:mma MMM d, yyyy")
.parseDateTime(date); //In (formatToYesterdayOrToday())
我使用过Joda-time.jar
答案 0 :(得分:3)
您可以尝试以下代码:( delta是以毫秒为单位的时间)
public static String getDisplayableTime(long delta)
{
long difference=0;
Long mDate = java.lang.System.currentTimeMillis();
if(mDate > delta)
{
difference= mDate - delta;
final long seconds = difference/1000;
final long minutes = seconds/60;
final long hours = minutes/60;
final long days = hours/24;
final long months = days/31;
final long years = days/365;
if (seconds < 0)
{
return "not yet";
}
else if (seconds < 60)
{
return seconds == 1 ? "one second ago" : seconds + " seconds ago";
}
else if (seconds < 120)
{
return "a minute ago";
}
else if (seconds < 2700) // 45 * 60
{
return minutes + " minutes ago";
}
else if (seconds < 5400) // 90 * 60
{
return "an hour ago";
}
else if (seconds < 86400) // 24 * 60 * 60
{
return hours + " hours ago";
}
else if (seconds < 172800) // 48 * 60 * 60
{
return "yesterday";
}
else if (seconds < 2592000) // 30 * 24 * 60 * 60
{
return days + " days ago";
}
else if (seconds < 31104000) // 12 * 30 * 24 * 60 * 60
{
return months <= 1 ? "one month ago" : days + " months ago";
}
else
{
return years <= 1 ? "one year ago" : years + " years ago";
}
}
return null;
}
答案 1 :(得分:3)
对于Android,您可以使用Joda-Time-Android库的最简单方式:
Date yourTime = new Date();
DateTime dateTime = new DateTime(yourTime); //or simple DateTime.now()
final String result = DateUtils.getRelativeTimeSpanString(getContext(), dateTime);
答案 2 :(得分:2)
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTime datetime = formatter.parseDateTime(time); // time = string time data
LocalDate localDate = new LocalDateTime(datetime).toLocalDate();
LocalDate localDateNow = new LocalDateTime().toLocalDate();
int diff = Days.daysBetween(localDate, localDateNow).getDays();
然后使用差异来评估哪一天。
答案 3 :(得分:2)
现在可以使用Android DateUtils中的方法轻松实现以下目的。
DateUtils.getRelativeTimeSpanString(feedItem.timeStamp)
您可以获得类似的输出,
1小时前, 昨天, 2天前
答案 4 :(得分:1)
在你的DateTimeFormatter中,它需要某种格式的日期,即"EEE hh:mma MMM d, yyyy"
:
DateTime dateTime = DateTimeFormat.forPattern("EEE hh:mma MMM d, yyyy")
.parseDateTime(date);
但是你传递的是"MM/dd/yyyy"
格式:
SimpleDateFormat outputFormat1 = new SimpleDateFormat("MM/dd/yyyy");
op = outputFormat1.format(d);
op = formatToYesterdayOrToday(op);
所以,你可以做的是改变你的DateTimeFormatter以期望你的初始格式"MM/dd/yyyy"
:
DateTime dateTime = DateTimeFormat.forPattern("MM/dd/yyyy")
.parseDateTime(date);
这应该可以解决你得到的错误。我不知道最后整件事情是否能给你你想要的东西(&#34;今天&#34;,&#34;昨天&#34;,&#34; 2天前&#34; ,. ..)。
答案 5 :(得分:0)
我使用以下函数来获取RemainingTime,但格式几乎与您想要的相同。尝试此代码并根据您的要求进行更改。
/**
* To convert expire time according to selected format(period)
* @param period
* @return
*/
public String getExpireDate(int period) {
int days = 0, weeks = 0, hours = 0, mins = 0, secs = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date systemDate = Calendar.getInstance().getTime();
String myDate = sdf.format(systemDate);
long millse = // your time in milliseconds
if (millse < 0) {
return activity.getResources().getString(R.string.already_expire);
}
long mills = Math.abs(millse);
if (mills > 60480000) {
weeks = (int) (mills / 604800000);
mills = mills % 604800000;
days = (int) (mills / 86400000);
mills = mills % 86400000;
hours = (int) (mills / 3600000);
mills = mills % 3600000;
mins = (int) (mills / 60000);
mills = mills % 60000;
secs = (int) (mills / 1000);
} else if (mills > 86400000) {
days = (int) (mills / 86400000);
mills = mills % 86400000;
hours = (int) (mills / 3600000);
mills = mills % 3600000;
mins = (int) (mills / 60000);
mills = mills % 60000;
secs = (int) (mills / 1000);
} else if (mills > 3600000) {
hours = (int) (mills / 3600000);
mills = mills % 3600000;
mins = (int) (mills / 60000);
mills = mills % 60000;
secs = (int) (mills / 1000);
}
String remainingTime = "";
switch (period) {
case SHORT_PERIOD:
if (weeks > 0) {
if (weeks == 1) {
remainingTime = weeks + " week";
} else {
remainingTime = weeks + " weeks";
}
} else if (days > 0) {
if (days == 1) {
remainingTime = days + " day";
} else {
remainingTime = days + " days";
}
} else if (hours > 0) {
if (hours == 1) {
remainingTime = hours + " hour";
} else {
remainingTime = hours + " hours";
}
} else if (mins > 0) {
if (mins == 1) {
remainingTime = mins + " minute";
} else {
remainingTime = mins + " minutes";
}
} else {
if (secs == 1) {
remainingTime = secs + " second";
} else {
remainingTime = secs + " seconds";
}
}
break;
case DETAILED_PERIOD:
if (weeks > 0) {
remainingTime = (weeks * 7) + days + " days," + hours
+ " hours," + mins + " minutes";// + secs + " secs";
} else if (days > 0) {
remainingTime = days + " days," + hours + " hours," + mins
+ " minutes";// + secs + " seconds";
} else if (hours > 0) {
remainingTime = hours + " hours," + mins + " minutes";// + secs
// +
// " seconds";
} else if (mins > 0) {
remainingTime = mins + " minutes";// + secs + " seconds";
}
break;
case EXACT_DATE_TIME:
SimpleDateFormat target = new SimpleDateFormat("dd-MMM-yyyy");
try {
remainingTime = target.format(sdf.parse(taskData
.getTask_expire_date()));
} catch (ParseException e) {
e.printStackTrace();
}
break;
default:
break;
}
return remainingTime;
}
答案 6 :(得分:0)
这应该提供你想要的输出。
使用SimpleDateFormat解析您的日期,然后使用以下
date.getTime()
;
long now = System.currentTimeMillis();
DateUtils.getRelativeTimeSpanString(your time in long, now, DateUtils.MINUTE_IN_MILLIS);
答案 7 :(得分:0)
假设您有这样的数据:
{"data":[{"id":"79tiyfjgdfg","sales_name":"Sengkuni","sales_branch":"Kingdom of Hastina","message":"Leader of Kurowo sent you a gold","timestamp":"2020-03-23 10:16:01"}]}
并调用如下函数:
holder.timestamp.setText(Utils.beautifyDate(context, notifications.get(position).getTimestamp(), "EEEE, MMMM d, YYYY"));
对于其他格式,您可以从https://developer.android.com/reference/java/text/SimpleDateFormat
查看在下面使用此功能
public static String beautifyDate(Context context, String timestamp, String formatDate) {
String beautifyFormat;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-M-dd H:mm:ss", Locale.getDefault());
Date date = new Date();
try {
date = fmt.parse(timestamp);
} catch (Exception e) {
e.printStackTrace();
}
String mYear = new SimpleDateFormat("yyyy", Locale.getDefault()).format(date);
String mMonth = new SimpleDateFormat("M", Locale.getDefault()).format(date);
String mDay = new SimpleDateFormat("dd", Locale.getDefault()).format(date);
String mHour = new SimpleDateFormat("H", Locale.getDefault()).format(date);
String mMinutes = new SimpleDateFormat("mm", Locale.getDefault()).format(date);
Calendar now = Calendar.getInstance();
int years = now.get(Calendar.YEAR);
int months = now.get(Calendar.MONTH) + 1;
int days = now.get(Calendar.DATE);
int hours = now.get(Calendar.HOUR_OF_DAY);
int minutes = now.get(Calendar.MINUTE);
if (mYear.equals(String.valueOf(years)) && mMonth.equals(String.valueOf(months)) && mDay.equals(String.valueOf(days))) {
Log.i("timestamp", "on same day");
hours -= Integer.parseInt(mHour);
beautifyFormat = hours > 1 ? hours + " " + context.getString(R.string.hours_ago): hours + " " + context.getString(R.string.hour_ago);
if (hours == 0) {
minutes -= Integer.parseInt(mMinutes);
beautifyFormat = minutes > 1 ? minutes + " " + context.getString(R.string.minutes_ago): context.getString(R.string.moments_ago);
}
} else {
days = now.get(Calendar.DATE) - Integer.parseInt(mDay);
if (days == 1) {
beautifyFormat = context.getString(R.string.tomorrow);
} else {
beautifyFormat = new SimpleDateFormat(formatDate, Locale.getDefault()).format(date);
}
}
return beautifyFormat;
}
只需调整您希望能够看到今天或明天以及其他时间的结果的时间
希望它有用。