import java.sql.Timestamp;
public class MainClass {
private static final long One_Day_In_Milliseconds = 86400000;
public static final String DATE_LABEL_TODAY = "Today";
public static final String DATE_LABEL_YESTERDAY = "Yesterday";
public static final String DATE_LABEL_THIS_MONTH = "This Month";
public static final String DATE_LABEL_OLDER = "Older";
public static final String DATE_LABEL_NONE = "";
public static java.sql.Timestamp getTimestamp() {
java.util.Date today = new java.util.Date();
return new java.sql.Timestamp(today.getTime());
}
public static String getDateLabel(java.sql.Timestamp ts, java.sql.Timestamp now) {
long tsTime = ts.getTime();
long nowTime = now.getTime();
long quotient = (nowTime - tsTime) / One_Day_In_Milliseconds;
if (quotient < 1) {
return DATE_LABEL_TODAY;
} else if (quotient < 2) {
return DATE_LABEL_YESTERDAY;
} else if (quotient < 30) {
return DATE_LABEL_THIS_MONTH;
} else {
return DATE_LABEL_OLDER;
}
}
public static void main(String[] args) {
java.sql.Timestamp now = getTimestamp();
java.sql.Timestamp ts1 = getTimestamp();
System.out.println(getDateLabel(ts1, now));
System.out.println(ts1.toString());
System.out.println("-------------");
// timestamp in format yyyy-mm-dd hh:mm:ss.fffffffff
java.sql.Timestamp ts22 = java.sql.Timestamp.valueOf("2007-01-06 09:01:10");
System.out.println(getDateLabel(ts22, now));
System.out.println(ts22.toString());
System.out.println("-------------");
java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2007-02-02 10:10:10");
System.out.println(getDateLabel(ts2, now));
System.out.println(ts2.toString());
System.out.println("-------------");
java.sql.Timestamp ts3 = java.sql.Timestamp.valueOf("2004-07-18 10:10:10");
System.out.println(getDateLabel(ts3, now));
System.out.println(ts3.toString());
System.out.println("-------------");
java.sql.Timestamp ts4 = java.sql.Timestamp.valueOf("2007-02-01 10:10:10");
System.out.println(getDateLabel(ts4, now));
System.out.println(ts4.toString());
System.out.println("-------------");
}
}
|