1.date转化为字符串
/**
* date转化为字符串
* @param messageDate
* @return
*/
public static String getTimestampString(Date messageDate) {
Locale curLocale = HXSDKHelper.getInstance().getAppContext().getResources().getConfiguration().locale;
String languageCode = curLocale.getLanguage();
boolean isChinese = languageCode.contains("zh");
String format = null;
long messageTime = messageDate.getTime();
if (isSameDay(messageTime)) {
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(messageDate);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
format = "HH:mm";
if (hour > 17) {
if (isChinese) {
format = "晚上 hh:mm";
}
} else if (hour >= 0 && hour <= 6) {
if (isChinese) {
format = "凌晨 hh:mm";
}
} else if (hour > 11 && hour <= 17) {
if (isChinese) {
format = "下午 hh:mm";
}
} else {
if (isChinese) {
format = "上午 hh:mm";
}
}
} else if (isYesterday(messageTime)) {
if (isChinese) {
format = "昨天 HH:mm";
} else {
format = "MM-dd HH:mm";
}
} else {
if (isChinese) {
format = "M月d日 HH:mm";
} else {
format = "MM-dd HH:mm";
}
}
if (isChinese) {
return new SimpleDateFormat(format, Locale.CHINA).format(messageDate);
} else {
return new SimpleDateFormat(format, Locale.US).format(messageDate);
}
}
- 时间格式化输出为字符串
/**
* 时间格式化输出为字符串
* @param dateStr
* @param formatStr
* @return
*/
public static Date StringToDate(String dateStr, String formatStr) {
DateFormat format = new SimpleDateFormat(formatStr);
Date date = null;
try {
date = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
3.获取日期年份
/**
* 获取日期年份
*
* @param date 日期
* @return
*/
public static String getYear(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date).substring(0, 4);
}
4.获取日期月份
/**
* 功能描述:返回月
*
* @param date Date 日期
* @return 返回月份
*/
public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
}
5.获取日期天
/**
* 功能描述:返回日
*
* @param date Date 日期
* @return 返回日份
*/
public static int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
6.获取日期的时分秒等
/**
* 功能描述:返回小
*
* @param date 日期
* @return 返回小时
*/
public static int getHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
/**
* 功能描述:返回分
*
* @param date 日期
* @return 返回分钟
*/
public static int getMinute(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MINUTE);
}
/**
* 返回秒钟
*
* @param date Date 日期
* @return 返回秒钟
*/
public static int getSecond(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.SECOND);
}
/**
* 功能描述:返回毫
*
* @param date 日期
* @return 返回毫
*/
public static long getMillis(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis();
}
7.获取日期的星期几
/**
* 获取日期的星期几
* @param time
* @return
*/
public static String getWeek(Long time){
Date date = new Date(time);
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
return dateFm.format(date);
}