我使用了getRelativeDateTimeString,它显示了最长1周的时间。
如何增加1周以上。
经过一段时间后,它显示正常的日期格式,例如11/14 / 2007,8:20 AM,我想在Nov/14/2007, 8:20 AM
或14 Nov 2007, 8:20 AM
这样的日期中显示月份名称< / p>
我希望输出为:Nov/14/2007, 8:20 AM
,但输出为11/14/2007, 8:20 AM
java.util.Date time = new java.util.Date((long) time1 * 1000);
int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | FORMAT_ABBREV_MONTH;
CharSequence mdate = DateUtils.getRelativeDateTimeString(context,time.getTime() , DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, flags);
答案 0 :(得分:1)
以下类可用于从时间戳中提取日期:
public class DateUtils {
public static final String DF_dd_MM_yyyy = "dd/MM/yyyy";
public static final String DF_MM_dd_yyyy = "MM/dd/yyyy";
public static final String DF_MMM_dd_yyyy = "MMM dd,yyyy";
public static final String DF_dd_MMM_yyyy = "dd/MMM/yyyy";
public static final String DF_MMMM_dd_yyyy = "MMMM dd, yyyy";
public static final String DF_MMMM_dd_yyyy_AT_hh_mm_a = "MMM/dd/yyyy, hh:mm a";
public static final String DF_dd_MM_yyyy_HH_mm_ss = "dd/MM/yyyy HH:mm:ss";
public static final String DF_HH_mm_a = "HH:mm a";
public static final String DF_hh_mm_a = "hh:mm a";
public static final String DF_MM_dd_yyyy_hh_mm_a = "MM/dd/yyyy hh:mm a";
public static final String DF_yyyy_MM_dd__HH_mm_ss = "yyyy-MM-dd kk:mm:ss";
public static final String DF_yyyy_mm_dd_T_hh_mm_s = "yyyy-MM-dd HH:mm:ss";//2017-10-10 T 10:10:10
public static final int SPLI_DATE = 101;
public static final int SPLI_MONTH = 102;
public static final int SPLI_YEAR = 103;
public static final String E_MM_DD = "EEEE MM/dd ";
public static final int DAY_SECONDS = 86400;
public static final int HOUR_SECONDS = 3600;
public static final int DAY_MILISECONDS = 86400000;
public static final int HOUR_MILISECONDS = 3600000;
private SimpleDateFormat dateFormatter;
private String strDateFormat;
private String strTimeFormat;
private String strDateTimeFormat;
private String strStringDate;
private long milliSeconds;
private TimeZone timeZone;
public static long getCurrentTimeInSecond() {
return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
}
public static long convertMilliSecondToSecond(long milliSeconds) {
return TimeUnit.MILLISECONDS.toSeconds(milliSeconds);
}
public static long convertMilliSecondToHours(long milliSeconds) {
return TimeUnit.MILLISECONDS.toSeconds(milliSeconds);
}
private static long getOffsetFromUtc() {
TimeZone tz = TimeZone.getDefault();
Date now = new Date();
return tz.getOffset(now.getTime());
}
public static class Builder {
DateUtils dateUtils;
public Builder() {
dateUtils = new DateUtils();
dateUtils.timeZone = TimeZone.getDefault();
}
public Builder setDateFormat(String strDateFormat) {
dateUtils.strDateFormat = strDateFormat;
return this;
}
public Builder setTimeFormat(String strTimeFormat) {
dateUtils.strTimeFormat = strTimeFormat;
return this;
}
public Builder setDateTimeFormat(String strDateTimeFormat) {
dateUtils.strDateTimeFormat = strDateTimeFormat;
return this;
}
public Builder setSeconds(long seconds) {
dateUtils.milliSeconds = seconds * 1000;
return this;
}
public Builder setTimeZone(TimeZone timeZone) {
dateUtils.timeZone = timeZone;
return this;
}
public Builder setUTCTimeZone() {
dateUtils.timeZone = TimeZone.getTimeZone("UTC");
return this;
}
public Builder setStringDate(String strDate, String strDateFormat) throws ParseException {
dateUtils.strStringDate = strDate;
dateUtils.strDateFormat = strDateFormat;
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strDateFormat);
dateUtils.milliSeconds = dateUtils.dateFormatter.parse(dateUtils.strStringDate).getTime();
return this;
}
public Builder setStringTime(String strDate, String strTimeFormat) throws ParseException {
dateUtils.strStringDate = strDate;
dateUtils.strTimeFormat = strTimeFormat;
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strTimeFormat);
dateUtils.milliSeconds = dateUtils.dateFormatter.parse(dateUtils.strStringDate).getTime();
return this;
}
public Builder setStringDateTime(String strDate, String strDateTimeFormat) throws ParseException {
dateUtils.strStringDate = strDate;
dateUtils.strDateTimeFormat = strDateTimeFormat;
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strDateTimeFormat);
dateUtils.milliSeconds = dateUtils.dateFormatter.parse(dateUtils.strStringDate).getTime();
return this;
}
public Builder now() {
dateUtils.milliSeconds = System.currentTimeMillis();
return this;
}
public Builder addHour(int hour) {
dateUtils.milliSeconds += hour * 60 * 60 * 1000;
return this;
}
public Builder addMinutes(int minutes) {
dateUtils.milliSeconds += minutes * 60 * 1000;
return this;
}
public Builder addSeconds(int seconds) {
dateUtils.milliSeconds += seconds * 1000;
return this;
}
public Builder addMiliSeconds(int milliSeconds) {
dateUtils.milliSeconds += milliSeconds;
return this;
}
public Builder minusHour(int hour) {
dateUtils.milliSeconds -= hour * 60 * 60 * 1000;
return this;
}
public Builder minusMinutes(int minutes) {
dateUtils.milliSeconds -= minutes * 60 * 1000;
return this;
}
public Builder minusSeconds(int seconds) {
dateUtils.milliSeconds -= seconds * 1000;
return this;
}
public Builder minusMiliSeconds(int milliSeconds) {
dateUtils.milliSeconds -= milliSeconds;
return this;
}
public String getStringDate() throws ParseException {
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strDateFormat);
return dateUtils.dateFormatter.format(getMilliSeconds());
}
public long getDateInSecond() throws ParseException {
/*if (TextUtils.isEmpty(dateUtils.strDateFormat))
dateUtils.strDateFormat = DateUtils.DF_dd_MM_yyyy;
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strDateFormat);
Date date = dateUtils.dateFormatter.parse(dateUtils.dateFormatter.format(getMilliSeconds()));
return date.getTime() / 1000;*/
return (getMilliSeconds() - (getMilliSeconds() % DAY_MILISECONDS)) / 1000;
}
public long getHourInSecond() throws ParseException {
return (getMilliSeconds() - (getMilliSeconds() % HOUR_MILISECONDS)) / 1000;
}
public Date getDate() throws ParseException {
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strDateFormat);
return new Date(getMilliSeconds());
}
public String getStringTime() throws ParseException {
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strTimeFormat);
return dateUtils.dateFormatter.format(getMilliSeconds());
}
public Date getTime() throws ParseException {
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strTimeFormat);
return new Date(getMilliSeconds());
}
@SuppressLint("SimpleDateFormat")
public String getSpitedDate(long timestamp, int type) {
Calendar cal = null;
try {
cal = new Builder().setSeconds(timestamp).getCalendar();
} catch (ParseException e) {
e.printStackTrace();
}
switch (type) {
case SPLI_DATE:
return cal.get(Calendar.DATE) + "";
case SPLI_MONTH:
SimpleDateFormat month_date = new SimpleDateFormat("MMM");
return month_date.format(cal.getTime());
case SPLI_YEAR:
return cal.get(Calendar.YEAR) + "";
}
return "";
}
public String getStringDateTime() throws ParseException {
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strDateTimeFormat);
return dateUtils.dateFormatter.format(getMilliSeconds());
}
public Date getDateTime() throws ParseException {
dateUtils.dateFormatter = getSimpleDateFormatInstance(dateUtils.strDateTimeFormat);
return new Date(getMilliSeconds());
}
public Calendar getCalendar() throws ParseException {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(dateUtils.timeZone);
cal.setTimeInMillis(getMilliSeconds());
return cal;
}
public long getMilliSeconds() {
return dateUtils.milliSeconds;
}
public Builder setMilliSeconds(long milliSeconds) {
dateUtils.milliSeconds = milliSeconds;
return this;
}
public long getDateTimeStampInSecond() {
return convertMilliSecondToSecond(dateUtils.milliSeconds);
}
public long getUTCDateTimeStampInSecond() throws ParseException {
return ((dateUtils.milliSeconds - getOffsetFromUtc()) / 1000);
}
private SimpleDateFormat getSimpleDateFormatInstance(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
sdf.setTimeZone(dateUtils.timeZone);
sdf.setLenient(false);
return sdf;
}
}
以下代码片段显示了如何使用该类:
String date = new DateUtils.Builder().setSeconds(timeStamp).setDateFormat(DateUtils.DF_MMMM_dd_yyyy_AT_hh_mm_a).getStringDate();
答案 1 :(得分:1)
您可以将其分为两部分:
DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
getRelativeTimeSpanString
希望有帮助,谢谢