Get a Date Label from a java.sql.Timestamp Object : Date Time Timestamp « Database « Java Tutorial






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("-------------");
  }
}
Today
2007-02-02 13:23:02.933
-------------
This Month
2007-01-06 09:01:10.0
-------------
Today
2007-02-02 10:10:10.0
-------------
Older
2004-07-18 10:10:10.0
-------------
Yesterday
2007-02-01 10:10:10.0
-------------








20.20.Date Time Timestamp
20.20.1.Working with Date, Time, and Timestamp in JDBC
20.20.2.Insert Date value
20.20.3.Get Date value from ResultSet
20.20.4.Insert TIME value
20.20.5.Get TIME value from ResultSet
20.20.6.Insert TIMESTAMP value
20.20.7.Get TIMESTAMP value from ResultSet
20.20.8.Get the Current Date as a java.util.Date Object
20.20.9.Creating java.sql.Date Using java.util.Date
20.20.10.Creating java.sql.Date Using java.util.Calendar
20.20.11.Creating java.sql.Date Using the java.sql.Date.valueOf() Method
20.20.12.Creating java.sql.Date Using GregorianCalendar
20.20.13.Getting a Timestamp Object Using java.util.Date
20.20.14.Getting a Timestamp Object Using System.currentTimeMillis()
20.20.15.Get the Current Timestamp as a java.sql.Time Object?
20.20.16.Convert a String Date Such as 2003/01/10 to a java.sql.Date Object?
20.20.17.Convert a Timestamp to Month-Day-Year
20.20.18.Get a Date Label from a java.sql.Timestamp Object
20.20.19.Convert a java.sql.Timestamp Object to a java.util.Date Object?
20.20.20.Make a java.sql.Timestamp Object for a Given Year, Month, Day, Hour
20.20.21.Get a Date for a Specific Time Zone
20.20.22.Returns a java.sql.Timestamp equal to the current time
20.20.23.Convert into java.sql.Time into java.util.Calendar