Examples: DateTime class

  1. This agent creates a DateTime object that represents 4 March 2005 at 6:07:08 AM. The use of java.util.Calendar eliminates dependencies on regional settings.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
          // (Your code goes here) 
          java.util.Calendar jdt = java.util.Calendar.getInstance();
          jdt.set(2005, 2, 4, 6, 7, 8);
          DateTime dt = session.createDateTime(jdt);
          System.out.println("Local time = " + dt.getLocalTime());
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  2. This agent creates a NotesDateTime object that represents 4 March 2005 at 6:07:08 if the regional setting is appropriate, for example, M/d/yy h:mm:ss. When you set the date from a String, you must be sure that the regional settings of the user's computer are appropriate to the String value.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
          // (Your code goes here) 
          if ((session.getInternational().isDateMDY()) &&
          (session.getInternational().getDateSep().equals("/")) &&
          (session.getInternational().getTimeSep().equals(":"))) {
            DateTime dt = session.createDateTime("3/4/05 6:07:08");
            System.out.println("Local time = " + dt.getLocalTime());
          }
          else
            System.out.println("Date format not MDY with / and :");
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  3. This agent creates a new DateTime object that represents today's date. The time component of the DateTime object is not set.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = 
          session.getAgentContext();
          // (Your code goes here) 
          DateTime dt = session.createDateTime("Today");
          System.out.println(dt.getLocalTime());
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  4. This agent creates a new DateTime object that represents the current date and time.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = 
          session.getAgentContext();
          // (Your code goes here) 
          DateTime dt = session.createDateTime("Today");
          dt.setNow();
          System.out.println(dt.getLocalTime());
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  5. This agent gets the value of the PurgeDate item in a document and places it into a DateTime object. The time zone setting of PurgeDate is preserved. For example, if PurgeDate has a value of 03/21/96 04:54:33 PM in Eastern Standard Time, the DateTime object represents 03/21/96 04:54:33 PM and its TimeZone property equals 5.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
          // (Your code goes here) 
          Document doc = agentContext.getDocumentContext();
          Item item = doc.getFirstItem("PurgeDate");
          DateTime dt = item.getDateTimeValue();
          System.out.println("Local time = " + 
            dt.getLocalTime() + " TZ" + dt.getTimeZone());
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }