Get Difference Between Two Dates

[quote]

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class BaseModel {
/**
* @Des for view mode to show friendly record date.
* @param modTs(record data time)
* @return Today, Yesterday, More than 1 week ...
*/
public String getFriendStr(Timestamp modTs) {
Date todayDate = new Date();
Date recordDate = new Date();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat();
df.applyPattern("yyyy-MM-dd");
try {
recordDate = df.parse(modTs.toString());
todayDate = df.parse(todayDate.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String friendlyStr = "";
int countDays = this.daysBetween(recordDate,todayDate);
int countMonths = this.getDateDif(recordDate,todayDate)[1];

if(countDays>100 || countMonths>2){
friendlyStr = "More than 3 months";
}else if(countMonths == 1 || countMonths == 2){
friendlyStr = "More than 1 month";
}else if(countDays >= 14){
friendlyStr = "More than 2 weeks";
}else if(countDays >= 7){
friendlyStr = "More than 1 week";
}else if(countDays >= 4 && countDays = 3){
friendlyStr = "3 days before";
}else if(countDays >= 1){
friendlyStr = "Yesterday";
}else {
friendlyStr = "Today";
/*df.applyPattern("HH:mm:ss");
friendlyStr = df.format(modTs);*/
}
return friendlyStr;
}
/**
* @Des: Get Difference Between Two Dates in Days, Months and Year format.
* @param date1
* @param date2
* @return
*/
public int[] getDateDif(Date date1, Date date2) {
int[] monthDay = { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Calendar fromDate;
Calendar toDate;
int increment = 0;
int[] ageDiffArr = new int[3];
int year;
int month;
int day;
Calendar d1 = new GregorianCalendar().getInstance();
d1.setTime(date1);
Calendar d2 = new GregorianCalendar().getInstance();
d2.setTime(date2);
if (d1.getTime().getTime() > d2.getTime().getTime()) {
fromDate = d2;
toDate = d1;
} else {
fromDate = d1;
toDate = d2;
}
if (fromDate.get(Calendar.DAY_OF_MONTH) > toDate.get(Calendar.DAY_OF_MONTH)) {
increment = monthDay[fromDate.get(Calendar.MONTH)];
}
GregorianCalendar cal = new GregorianCalendar();
boolean isLeapYear = cal.isLeapYear(fromDate.get(Calendar.YEAR));
if (increment == -1) {
if (isLeapYear) {
increment = 29;
} else {
increment = 28;
}
}
// DAY CALCULATION
if (increment != 0) {
day = (toDate.get(Calendar.DAY_OF_MONTH) + increment) - fromDate.get(Calendar.DAY_OF_MONTH);
increment = 1;
} else {
day = toDate.get(Calendar.DAY_OF_MONTH) - fromDate.get(Calendar.DAY_OF_MONTH);
}
// MONTH CALCULATION
if ((fromDate.get(Calendar.MONTH) + increment) > toDate.get(Calendar.MONTH)) {
month = (toDate.get(Calendar.MONTH) + 12) - (fromDate.get(Calendar.MONTH) + increment);
increment = 1;
} else {
month = (toDate.get(Calendar.MONTH)) - (fromDate.get(Calendar.MONTH) + increment);
increment = 0;
}
// YEAR CALCULATION
year = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR) + increment);
ageDiffArr[0] = day;
ageDiffArr[1] = month;
ageDiffArr[2] = year;
return ageDiffArr; // RESULT AS DAY, MONTH AND YEAR in form of Array
}
/**
* @Des Get Difference Between Two Dates in Days (Total days)
* @param early
* @param late
* @return
*/
public int daysBetween(Date early, Date late) {
java.util.Calendar calst = java.util.Calendar.getInstance();
java.util.Calendar caled = java.util.Calendar.getInstance();
calst.setTime(early);
caled.setTime(late);
// init set time to zero.
calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
calst.set(java.util.Calendar.MINUTE, 0);
calst.set(java.util.Calendar.SECOND, 0);
caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
caled.set(java.util.Calendar.MINUTE, 0);
caled.set(java.util.Calendar.SECOND, 0);
// get between tow one.
int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
return days;
}
}

[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write a C++ program that defines a class DateV2 that (1) Contains all the members in the class DateV1; Programming for Engineers C++ (2) Has two constructors as follows: One takes three parameters, int y, int m, int n; The other is the default constructor that takes no parameter (3) Has additional public member functions as follows: string getWeekDay(); // return the week day, for example, Sunday if day is 0, etc bool Leap(); // return if the year is leap int differFrom(DateV2& oneDate); // return the difference in days between the calling object // and the oneDate object void printDate(); // print the year, the month in English, the day, and the week day Test class DateV2 in the main function as follows: (1) Declare and set the objects today and tomorrow as in Problem 2. (2) Declare and initialize (by a constructor) an object to represent your OWN birthday. (3) Use the member function printDate to print today, tomorrow, and your birthday. (4) Output the weekday of today, tomorrow, and your own birthday. (5) Output how many days has passed since your birth (the difference between your birthday and today). Hint: i) We can use another string array to store the English name for week days (Sunday, Monday, through Saturday) ii) We know that it is Monday on Year 1, Month 1, and Day 1 iii) A good idea is to first design a function to compute the number of days that has passed since Year 1, Month 1, and Day 1, and then to use this function to compute the week day for a give date and to compute the difference between two dates. You can store the number of days for each of the 12 months in an integer array, which helps in counting the days.
最新发布
05-22

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值