Java date and time manipulation Notes

Submitted by code_admin on Mon, 07/23/2018 - 09:29

Date

  1.     java.util.Date minDate = new java.util.GregorianCalendar(2010,java.util.GregorianCalendar.MARCH,01).getTime(); // 1st mar 2010
  2.     java.util.Date maxDate = new java.util.GregorianCalendar(2020,java.util.GregorianCalendar.MARCH,01).getTime(); // 1st mar 2020
  3.  
  4.       java.util.Date defDate = new java.util.Date(); // Two months ago
  5.  
  6.       Calendar cal = Calendar.getInstance();
  7.       cal.add(Calendar.MONTH,-2);
  8.       defDate = cal.getTime();
  9.  
  10.       ti.setMinValue(minDate);
  11.       ti.setMaxValue(maxDate);
  12.       ti.setValue(defDate);

Use c.add(Calendar.DATE, 1); to add days

Add Year

  1.             Date nd = p_ItemLine.getDate();
  2.             java.text.DateFormat formatter = null;
  3.             formatter = new java.text.SimpleDateFormat("dd");
  4.             String sDay = formatter.format(nd);
  5.             formatter = new java.text.SimpleDateFormat("MM");
  6.             String sMon = formatter.format(nd);
  7.             formatter = new java.text.SimpleDateFormat("yyyy");
  8.             String sYear = formatter.format(nd);
  9.             Date newDate =  new java.util.GregorianCalendar(Integer.parseInt(sYear),Integer.parseInt(sMon)-1,Integer.parseInt(sDay),11,22).getTime();

Convert Date to Text

  1.       java.text.DateFormat formatter = new java.text.SimpleDateFormat("dd-MMM-yyyy");
  2.       String s = formatter.format(defDate);

Pause Delay Wait

  1.         Calendar cal = Calendar.getInstance();
  2.         cal.setTime(new Date());
  3.         cal.add(Calendar.SECOND,2);
  4.         while (new Date().compareTo(cal.getTime())<0) {};

To and From sqlite

  1.     protected String sqliteDateStatement(Date p_in) {
  2.         //YYYY-MM-DD HH:MM:SS.SSS
  3.         if (p_in==null) return "'NULL'";
  4.         SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  5.         return "DATETIME('" + formatter.format(p_in) + "')";      
  6.     }
  7.     protected Date sqliteDateVar(String p_in) throws ParseException {
  8.         //p_in format = 2012-09-30 16:33:41
  9.         if ("NULL".equals(p_in)) return null;
  10.         SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
  11.         return (Date) formatter.parse(p_in);
  12.     }

To and From xml

(Using javax.xml.bind.DatatypeConverter)

  1.     protected Date xmlDateVar(String p_in) throws ParseException {
  2.         if ("NULL".equals(p_in)) return null;
  3.         Calendar cal = DatatypeConverter.parseDateTime(p_in);
  4.         return cal.getTime();
  5.         //SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
  6.         //return (Date) formatter.parse(p_in);
  7.     }
  8.     protected String xmlDateString(Date p_in) {
  9.         //XML Date format = 2008-10-31T15:07:38.6875000-05:00
  10.         if (p_in==null) return "'NULL'";
  11.         Calendar cal=Calendar.getInstance();
  12.         cal.setTime(p_in);        
  13.         return DatatypeConverter.printDateTime(cal);
  14.         //SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
  15.         //return formatter.format(p_in);
  16.     }

Cheat Sheet http://www.ecotronics.ch/webdesign/javadate.htm

Formatting

  1.         SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

Timeing

  1. protected long m_intivalIncrement = TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS);
  2. long now = System.currentTimeMillis()

Tags

RJM Article Type
Work Notes