关于时间显示为今天、昨天、星期一之类的格式问题
2016-03-03 17:20
477 查看
时间格式统一用年月日判断yyyy-MM-dd,因为时分秒处理起来有些识别上的问题,如 第一个时间是 2016-03-02 22:00::00 第二个时间为 2016-03-03 8:00::00
第一个时间相对于第二个时间是昨天还是今天?
下面是代码:
/**
* 将时间转化为本地星期
* @author Administrator
*
*/
public class DateTest1 {
public static void main(String[] args) throws ParseException {
String currentTime = String.format("%tF%n", new Date(System.currentTimeMillis()));
System.out.println("当前时间:"+currentTime);
String result = getDayDiff("2016-02-28", currentTime);
System.out.println("显示的时间格式:"+result);
}
/**
* 得到时间差 yyyy-MM-dd 格式
* @param time
* @return
* @throws ParseException
*/
public static String getDayDiff(String time) throws ParseException{
String currentTime = String.format("%tF%n", new Date(System.currentTimeMillis()));
return getDayDiff(time, currentTime);
}
/**
* 得到时间差 yyyy-MM-dd 格式
* @param fDateStr 需要计算的时间
* @param oDateStr 应该传入当前时间
* @return
* @throws ParseException
*/
public static String getDayDiff(String fDateStr, String oDateStr) throws ParseException{
int result = daysOfTwo(fDateStr, oDateStr);
String timeResult = "";
switch (result) {
case -1:
timeResult = "请检查时间";
break;
case 0:
timeResult = "今天";
break;
case 1:
timeResult = "昨天";
break;
default:
timeResult = String.format("%tA%n", getDateFormat(fDateStr));
break;
}
if(Math.abs(result) > 7){//假如时间大于7天
timeResult = fDateStr;
}
return timeResult;
}
/**
* 判断时间相差几天
* @param fDate yyyy-MM-dd 格式
* @param oDate 应写当前时间
* @return 时间为-1时,请检查代码
* @throws ParseException
*/
public static int daysOfTwo(String fDateStr, String oDateStr) throws ParseException {
Date fDate = getDateFormat(fDateStr);
Date oDate = getDateFormat(oDateStr);
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(fDate);
int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
aCalendar.setTime(oDate);
int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
return day2 - day1;
}
/**
* 将时间转换为 Date类型
* @param time yyyy-MM-dd格式
* @return
* @throws ParseException
*/
public static Date getDateFormat(String time) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(time);
}
}
第一个时间相对于第二个时间是昨天还是今天?
下面是代码:
/**
* 将时间转化为本地星期
* @author Administrator
*
*/
public class DateTest1 {
public static void main(String[] args) throws ParseException {
String currentTime = String.format("%tF%n", new Date(System.currentTimeMillis()));
System.out.println("当前时间:"+currentTime);
String result = getDayDiff("2016-02-28", currentTime);
System.out.println("显示的时间格式:"+result);
}
/**
* 得到时间差 yyyy-MM-dd 格式
* @param time
* @return
* @throws ParseException
*/
public static String getDayDiff(String time) throws ParseException{
String currentTime = String.format("%tF%n", new Date(System.currentTimeMillis()));
return getDayDiff(time, currentTime);
}
/**
* 得到时间差 yyyy-MM-dd 格式
* @param fDateStr 需要计算的时间
* @param oDateStr 应该传入当前时间
* @return
* @throws ParseException
*/
public static String getDayDiff(String fDateStr, String oDateStr) throws ParseException{
int result = daysOfTwo(fDateStr, oDateStr);
String timeResult = "";
switch (result) {
case -1:
timeResult = "请检查时间";
break;
case 0:
timeResult = "今天";
break;
case 1:
timeResult = "昨天";
break;
default:
timeResult = String.format("%tA%n", getDateFormat(fDateStr));
break;
}
if(Math.abs(result) > 7){//假如时间大于7天
timeResult = fDateStr;
}
return timeResult;
}
/**
* 判断时间相差几天
* @param fDate yyyy-MM-dd 格式
* @param oDate 应写当前时间
* @return 时间为-1时,请检查代码
* @throws ParseException
*/
public static int daysOfTwo(String fDateStr, String oDateStr) throws ParseException {
Date fDate = getDateFormat(fDateStr);
Date oDate = getDateFormat(oDateStr);
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(fDate);
int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
aCalendar.setTime(oDate);
int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
return day2 - day1;
}
/**
* 将时间转换为 Date类型
* @param time yyyy-MM-dd格式
* @return
* @throws ParseException
*/
public static Date getDateFormat(String time) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(time);
}
}
相关文章推荐
- Android 检验当前网络是否可用
- python 装饰器 上下文管理器 迭代器 生成器 描述符
- python开发_常用的python模块及安装方法
- linden
- Virtualization-Cpu/Memory/IO虚拟化详解
- Jquery面试题整合
- setTimeout延时0毫秒的作用和问题
- 序列化与反序列化
- js闭包
- 大数据测试之hadoop命令大全
- 老李分享:接口测试之jmeter
- linux 下的进程管理工具 supervisor
- python——正则表达式
- Git 学习笔记(四)
- POJ百炼-2972-确定进制
- Unix timestamp时间 与 普通时间 转换方法
- drawable文件夹详解
- Nginx配置文件各配置项详细说明
- gdb调试命令
- 用户、组或角色 'zgb' 在当前数据库中已存在。 (Microsoft SQL Server,错误: 15023)