Java Date Cheat Sheet  (Oracle flavour)

Date and Time in Java are pretty simple, once you have understood some basic rules:As I said, once you have understood these really simple and intuitive rules (which means in about the half-life of uranium 234), working with date values in java is really a piece of cake (I don't know why this reminds my of the legendary dwarf bread of Terry Pratchett).
OK, nun hab ich genug gelästert, let's do some work:
 
Task java.util.Date java.util.Calendar java.sql.Date oracle.jbo.domain.Date
Inheritance
java.lang.Object
  java.util.Date
java.lang.Object
  java.util.Calendar (abstract)
    java.util.GregorianCalendar
java.lang.Object
  java.util.Date
    java.sql.Date
java.lang.Object
  oracle.sql.Datum (1)
    oracle.sql.DATE
      oracle.jbo.domain.Date
Current date and time
public java.util.Date 
  getUtilDateNow() {
  return new java.util.Date();
}
public java.util.Calendar 
  getUtilCalendarNow() {
  return java.util.Calendar.getInstance();
}
-
SQL Date values do not have 
a time component
public oracle.jbo.domain.Date getJboNow() {
  return new oracle.jbo.domain.Date(
    new java.sql.Timestamp(
       System.currentTimeMillis()));
}
Current date
public java.util.Date getUtilDateToday() {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("dd.MM.yyyy");
  String datestring = 
    sdf.format(new java.util.Date());
  java.util.Date today = null;
  try {
    today = sdf.parse(datestring);
  } catch (Exception exc) {
    System.out.println("Date error");
  }
  return today;
}
public java.util.Calendar getUtilCalendarToday() {
  java.util.Calendar now =
    new java.util.GregorianCalendar();
  java.util.Calendar today =
    new java.util.GregorianCalendar(
      now.get(java.util.Calendar.YEAR),
      now.get(java.util.Calendar.MONTH),
      now.get(java.util.Calendar.DATE));
  return today;
}
public java.sql.Date getSqlToday() {
  return new java.sql.Date(
    System.currentTimeMillis());
}
public oracle.jbo.domain.Date getJboToday() {
  return new oracle.jbo.domain.Date(
    oracle.jbo.domain.Date.getCurrentDate());
}
Date value for yesterday
public java.util.Date getUtilDateYesterday() {
  java.util.Calendar now =
  new java.util.GregorianCalendar();
  java.util.Calendar cal =
    new java.util.GregorianCalendar(
      now.get(java.util.Calendar.YEAR),
      now.get(java.util.Calendar.MONTH),
      now.get(java.util.Calendar.DATE));
  cal.add(java.util.Calendar.DAY_OF_MONTH, -1);
  return cal.getTime();
}
public java.util.Date getUtilCalendarYesterday() {
  java.util.Calendar now =
  new java.util.GregorianCalendar();
  java.util.Calendar cal =
    new java.util.GregorianCalendar(
      now.get(java.util.Calendar.YEAR),
      now.get(java.util.Calendar.MONTH),
      now.get(java.util.Calendar.DATE));
  cal.add(java.util.Calendar.DAY_OF_MONTH, -1);
  return cal;
}
public java.util.Date getSqlYesterday() {
  java.util.Calendar now =
  new java.util.GregorianCalendar();
  java.util.Calendar cal =
    new java.util.GregorianCalendar(
      now.get(java.util.Calendar.YEAR),
      now.get(java.util.Calendar.MONTH),
      now.get(java.util.Calendar.DATE));
  cal.add(java.util.Calendar.DAY_OF_MONTH, -1);
  return new java.sql.Date(
    cal.getTimeInMillis());
}
public oracle.jbo.domain.Date getJboYesterday() {
  oracle.jbo.domain.Date tmp =
    new oracle.jbo.domain.Date(
      oracle.jbo.domain.Date.getCurrentDate());
  return new oracle.jbo.domain.Date(
    tmp.addJulianDays(-1,0));
}
Last date of month
public java.util.Date getUtilDateLastDateOfMonth(
  java.util.Date date) {
  java.util.GregorianCalendar cal = 
    new java.util.GregorianCalendar();
  cal.setTime(date);
  cal.set(java.util.Calendar.DAY_OF_MONTH,
    cal.getActualMaximum(
      java.util.Calendar.DAY_OF_MONTH));
  return cal.getTime();
}
public java.util.Calendar getUtilCalendarLastDateOfMonth(
  java.util.Calendar cal) {
  java.util.GregorianCalendar tmp =
    new java.util.GregorianCalendar();
  tmp.setTime(cal.getTime());
  cal.set(java.util.Calendar.DAY_OF_MONTH,
    cal.getActualMaximum(
      java.util.Calendar.DAY_OF_MONTH));
  return cal;
}
public java.sql.Date getSqlLastDateOfMonth(
  java.sql.Date sql) {
  java.util.GregorianCalendar cal =
    new java.util.GregorianCalendar();
  cal.setTimeInMillis(sql.getTime());
  cal.set(java.util.Calendar.DAY_OF_MONTH,
    cal.getActualMaximum(
      java.util.Calendar.DAY_OF_MONTH));
  return new java.sql.Date(cal.getTimeInMillis());
}
//beware of oracle.jbo.domain.Date.lastDayOfMonth,
//oracle forgot to implement it!
public oracle.jbo.domain.Date getJboLastDateOfMonth(
  oracle.jbo.domain.Date jbo) {
  java.util.GregorianCalendar cal =
    new java.util.GregorianCalendar();
  cal.setTime(jbo.dateValue());
  cal.set(java.util.Calendar.DAY_OF_MONTH,
    cal.getActualMaximum(
      java.util.Calendar.DAY_OF_MONTH));
  return new oracle.jbo.domain.Date(
    new java.sql.Date(cal.getTimeInMillis()));
}
Convert to formatted String
public static String formatUtilDateAsSimpleDate(
  java.util.Date date) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("dd.MM.yyyy");
  return sdf.format(date);
}
public static String formatUtilCalendarAsSimpleDate(
  java.util.Calendar cal) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("dd.MM.yyyy");
  return sdf.format(cal.getTime());
}
public static String formatSqlAsSimpleDate(
  java.sql.Date sql) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("dd.MM.yyyy");
  return sdf.format(sql.getTime());
}
public static String formatJboAsSimpleDate(
  oracle.jbo.domain.Date jbo) {
  java.text.SimpleDateFormat sdf =
  new java.text.SimpleDateFormat("dd.MM.yyyy");
    return sdf.format(jbo.getValue());
}
Convert from String
//datestring has to be something like 31.1.2008
public static java.util.Date
  convertStringToUtilDate(String datestring) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("dd.MM.yyyy");
  java.util.Date date = null;
  try {
    date = sdf.parse(datestring);
  } catch (Exception exc) {
    System.out.println("Date error");
  }
  return date;
}
//datestring has to be something like 31.1.2008
public static java.util.Calendar
  convertStringToUtilCalendar(String datestring) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("dd.MM.yyyy");
  java.util.Date date = null;
  java.util.Calendar cal = 
    new java.util.GregorianCalendar();
  try {
    date = sdf.parse(datestring);
    cal.setTime(date);
  } catch (Exception exc) {
    System.out.println("Date error");
  }
  return cal;
}
//datestring has to be something like 31.1.2008
public static java.sql.Date
  convertStringToSqlDate(String datestring) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("dd.MM.yyyy");
  java.text.SimpleDateFormat sdfsql =
    new java.text.SimpleDateFormat("yyyy-MM-dd");
  java.util.Date date = null;
  try {
    date = sdf.parse(datestring);
  } catch (Exception exc) {
    System.out.println("Date error");
  }
  return java.sql.Date.valueOf(
    sdfsql.format(date));
}
//datestring has to be something like 31.1.2008
public static oracle.jbo.domain.Date
  convertStringToJboDate(String datestring) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("dd.MM.yyyy");
  java.text.SimpleDateFormat sdfsql =
    new java.text.SimpleDateFormat("yyyy-MM-dd");
  java.util.Date date = null;
  try {
    date = sdf.parse(datestring);
  } catch (Exception exc) {
    System.out.println("Date error");
  }
  return new oracle.jbo.domain.Date(
    sdfsql.format(date));
}
Convert to java.util.Date
-
public static java.util.Date
  convertUtilCalendarToUtilDate(java.util.Calendar cal) {
  return cal.getTime();
}
public static java.util.Date
  convertSqlToUtilDate(java.sql.Date sql) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("dd.MM.yyyy");
  String datestring = sdf.format(
    new java.util.Date(sql.getTime()));
  java.util.Date date = null;
  try {
    date = sdf.parse(datestring);
  } catch (Exception exc) {
    System.out.println("Date error");
  }
  return date;
}
public static java.util.Date
  convertJboToUtilDate(oracle.jbo.domain.Date jbo) {
  return jbo.dateValue();
}
Convert to java.util.Calendar
public static java.util.Calendar
  convertUtilDateToCalendar(java.util.Date date) {
  java.util.Calendar cal = 
    new java.util.GregorianCalendar();
  cal.setTime(date);
  return cal;
}
-
public static java.util.Calendar
  convertSqlToUtilCalendar(java.sql.Date sql) {
  java.util.Calendar date =
    new java.util.GregorianCalendar();
  date.setTimeInMillis(sql.getTime());
  //remove the time that comes along
  java.util.Calendar cal =
    new java.util.GregorianCalendar(
      date.get(java.util.Calendar.YEAR),
      date.get(java.util.Calendar.MONTH),
      date.get(java.util.Calendar.DATE));
  return cal;
}
public static java.util.Calendar
  convertJboToUtilCalendar(oracle.jbo.domain.Date jbo) {
  java.util.Calendar cal =
    new java.util.GregorianCalendar();
  cal.setTime((java.sql.Date) jbo.dateValue());
  return cal;
}
Convert to java.sql.Date
public static java.sql.Date
  convertUtilDateToSqlDate(java.util.Date date) {
  return new java.sql.Date(date.getTime());
}
public static java.sql.Date
  convertUtilCalendarToSqlDate(java.util.Calendar cal) {
  return new java.sql.Date(cal.getTimeInMillis());
}
-
public static java.sql.Date
  convertJboToSqlDate(oracle.jbo.domain.Date jbo) {
  return jbo.dateValue();
}
Convert to oracle.jbo.domain.Date
public static oracle.jbo.domain.Date
  convertUtilDateToJboDate(java.util.Date date) {
  return new oracle.jbo.domain.Date(
    new java.sql.Date(date.getTime()));
}
public static oracle.jbo.domain.Date
  convertUtilCalendarToJboDate(java.util.Calendar cal) {
  return new oracle.jbo.domain.Date(
    new java.sql.Date(cal.getTimeInMillis()));
}
public static oracle.jbo.domain.Date
  convertSqlToJboDate(java.sql.Date sql) {
  return new oracle.jbo.domain.Date(sql);
}
-
Is a date before 
another one?
public boolean isDate1BeforeDate2(java.util.Date date1,
  java.util.Date date2) {
  return date1.before(date2);
}
public boolean isDate1BeforeDate2(
  java.util.Calendar date1,
  java.util.Calendar date2) {
  return date1.before(date2);
}
public boolean isDate1BeforeDate2(
  java.sql.Date date1,
  java.sql.Date date2) {
  return date1.before(date2);
}
public boolean isDate1BeforeDate2(
  oracle.jbo.domain.Date date1,
  oracle.jbo.domain.Date date2) {
  return (date1.compareTo(date2) == -1) ;
}
Long date string in a foreign Locale
public static String formatUtilDateAsLongLocaleDate(
  java.util.Date date) {
  java.util.Locale loc = new java.util.Locale("pt", "BR");
  java.text.DateFormat df =
    java.text.DateFormat.getDateInstance(
      java.text.DateFormat.LONG, loc);
  java.text.DateFormat dft =
    java.text.DateFormat.getTimeInstance(
      java.text.DateFormat.MEDIUM, loc);
  return df.format(date) + " " + dft.format(date);
}
public static String formatUtilCalendarAsLongLocaleDate(
  java.util.Calendar cal) {
  java.util.Locale loc = 
    new java.util.Locale("pt", "BR");
  java.text.DateFormat df =
    java.text.DateFormat.getDateInstance(
      java.text.DateFormat.LONG, loc);
  java.text.DateFormat dft =
    java.text.DateFormat.getTimeInstance(
      java.text.DateFormat.MEDIUM, loc);
  return df.format(cal.getTime()) + " " 
    + dft.format(cal.getTime());
}
public static String formatSqlAsLongLocaleDate(
  java.sql.Date sql) {
  java.util.Locale loc = 
    new java.util.Locale("pt", "BR");
  java.text.DateFormat df =
    java.text.DateFormat.getDateInstance(
      java.text.DateFormat.LONG, loc);
  java.text.DateFormat dft =
    java.text.DateFormat.getTimeInstance(
      java.text.DateFormat.MEDIUM, loc);
  return df.format(sql) + " " + dft.format(sql);
}
public static String formatJboAsLongLocaleDate(
  oracle.jbo.domain.Date jbo) {
  java.util.Locale loc = 
    new java.util.Locale("pt", "BR");
  java.text.DateFormat df =
    java.text.DateFormat.getDateInstance(
      java.text.DateFormat.LONG, loc);
  java.text.DateFormat dft =
    java.text.DateFormat.getTimeInstance(
      java.text.DateFormat.MEDIUM, loc);
  return df.format(jbo.dateValue()) + " " 
    + dft.format(jbo.timeValue());
}
Weekday as integer
//Week goes from Sunday=1 to Saturday=7
public int getWeekdayAsInt(java.util.Date date) {
  java.util.Calendar cal =
    new java.util.GregorianCalendar();
  cal.setTime(date);
  return cal.get(java.util.Calendar.DAY_OF_WEEK);
}
//Week goes from Sunday=1 to Saturday=7
public int getWeekdayAsInt(java.util.Calendar cal) {
  return cal.get(java.util.Calendar.DAY_OF_WEEK);
}
//Week goes from Sunday=1 to Saturday=7
public int getWeekdayAsInt(java.sql.Date sql) {
  java.util.Calendar cal =
    new java.util.GregorianCalendar();
  cal.setTimeInMillis(sql.getTime());
  return cal.get(java.util.Calendar.DAY_OF_WEEK);
}
//Week goes from Sunday=1 to Saturday=7
public int getWeekdayAsInt(oracle.jbo.domain.Date jbo) {
  java.util.Calendar cal =
    new java.util.GregorianCalendar();
  cal.setTime(jbo.dateValue());
  return cal.get(java.util.Calendar.DAY_OF_WEEK);
}
Weekday as Text
public String getWeekdayAsString(java.util.Date date) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("EEEE");
  return sdf.format(date);
}
public String getWeekdayAsString(java.util.Calendar cal) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("EEEE");
  return sdf.format(cal.getTime());
}
public String getWeekdayAsString(java.sql.Date sql) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("EEEE");
  return sdf.format(sql);
}
public String getWeekdayAsString(
  oracle.jbo.domain.Date jbo) {
  java.text.SimpleDateFormat sdf =
    new java.text.SimpleDateFormat("EEEE");
  return sdf.format(jbo.dateValue());
}

(1) Thanks god no Chinese programmer worked in this oracle project!


Diese Webseite wurde am 03.05.18 um 22:34 von rothen ecotronics erstellt oder überarbeitet.

Impressum

Zurück zu rothen ecotronics

Printed on 100% recycled electrons!