用 ...ago java 替换日期
Replacement of date with ...ago java
我已经开始在 java 中构建一个逻辑,它将采用 'dd-MM-yyyy HH:mm:ss' 格式的日期输入并按以下方式显示输出:
如果日期是 today/yesterday 那么它应该是:today/yesterday HH:mm am/pm
如果少于 7 天前:x 天前
如果超过 7 天前:上周
同样
两周前
三周前
一个月前(i.e.four 周前)
2 个月前
..
11 个月前
一年前
然后是 x 年前。
我需要的只是正确计算从今天开始的天数。
我遇到了 joda-time 让我的工作变得轻松,但我无法确定它是否有任何内置的东西可以给我想要的输出。
int days = Days.daysBetween(new LocalDate(), new LocalDate()).getDays();
我今天写了这段代码,但我不知道如何在 joda 中休息-time.Please 指导我。
我还是创建了 logic.Thanks!
`String convertDateToWords(String datestr) 抛出 java.text.ParseException{
//here datestr is in this format from mysql database: "2015-03-03 09:14:04.0";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date bDate = formatter.parse(datestr);
Calendar today = Calendar.getInstance();
Calendar date = Calendar.getInstance();
date.setTime(bDate);
String str = "";
String ampm = new SimpleDateFormat("hh:mm a").format(bDate);
int fromYear = today.get(Calendar.YEAR);
int toYear = date.get(Calendar.YEAR);
int fromMonth = today.get(Calendar.MONTH)+1;
int toMonth = date.get(Calendar.MONTH)+1;
date.set(Calendar.YEAR, today.get(Calendar.YEAR));
int fromDay = today.get(Calendar.DAY_OF_YEAR);
int toDay = date.get(Calendar.DAY_OF_YEAR);
if(fromYear!=toYear){
str = (fromYear-toYear)+" year(s) ago";
}else if(fromMonth!=toMonth){
str = (fromMonth-toMonth)+" month(s) ago";
}else{
int diff = fromDay - toDay;
if(diff == 0){
str = " today | "+ampm;
}
if(diff == 1){
str = " yesterday | "+ampm;
}
if(diff >=2 && diff <7){
str = " "+diff+" days ago";
str = getDay(date.get(Calendar.DAY_OF_WEEK))+" | "+ampm;
}
if(diff >=7 && diff <14){
str = " last week";
}
if(diff >=14 && diff <21){
str = " 2 weeks ago";
}
if(diff >=21 && diff <28){
str = " 3 weeks ago";
}
if(diff >=28){
str = " 4 weeks ago";
}
}
return str;
}
我已经开始在 java 中构建一个逻辑,它将采用 'dd-MM-yyyy HH:mm:ss' 格式的日期输入并按以下方式显示输出:
如果日期是 today/yesterday 那么它应该是:today/yesterday HH:mm am/pm
如果少于 7 天前:x 天前
如果超过 7 天前:上周
同样
两周前
三周前
一个月前(i.e.four 周前)
2 个月前
..
11 个月前
一年前
然后是 x 年前。
我需要的只是正确计算从今天开始的天数。
我遇到了 joda-time 让我的工作变得轻松,但我无法确定它是否有任何内置的东西可以给我想要的输出。
int days = Days.daysBetween(new LocalDate(), new LocalDate()).getDays();
我今天写了这段代码,但我不知道如何在 joda 中休息-time.Please 指导我。
我还是创建了 logic.Thanks! `String convertDateToWords(String datestr) 抛出 java.text.ParseException{
//here datestr is in this format from mysql database: "2015-03-03 09:14:04.0";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date bDate = formatter.parse(datestr);
Calendar today = Calendar.getInstance();
Calendar date = Calendar.getInstance();
date.setTime(bDate);
String str = "";
String ampm = new SimpleDateFormat("hh:mm a").format(bDate);
int fromYear = today.get(Calendar.YEAR);
int toYear = date.get(Calendar.YEAR);
int fromMonth = today.get(Calendar.MONTH)+1;
int toMonth = date.get(Calendar.MONTH)+1;
date.set(Calendar.YEAR, today.get(Calendar.YEAR));
int fromDay = today.get(Calendar.DAY_OF_YEAR);
int toDay = date.get(Calendar.DAY_OF_YEAR);
if(fromYear!=toYear){
str = (fromYear-toYear)+" year(s) ago";
}else if(fromMonth!=toMonth){
str = (fromMonth-toMonth)+" month(s) ago";
}else{
int diff = fromDay - toDay;
if(diff == 0){
str = " today | "+ampm;
}
if(diff == 1){
str = " yesterday | "+ampm;
}
if(diff >=2 && diff <7){
str = " "+diff+" days ago";
str = getDay(date.get(Calendar.DAY_OF_WEEK))+" | "+ampm;
}
if(diff >=7 && diff <14){
str = " last week";
}
if(diff >=14 && diff <21){
str = " 2 weeks ago";
}
if(diff >=21 && diff <28){
str = " 3 weeks ago";
}
if(diff >=28){
str = " 4 weeks ago";
}
}
return str;
}