Package java.util

Examples of java.util.GregorianCalendar


     */
    public static String serializeSqlDate(java.sql.Date date) {
       
        // values should be normalized to midnight in zone, but no guarantee
        //  so convert using the lame calendar approach
        GregorianCalendar cal = new GregorianCalendar();
        cal.setGregorianChange(BEGINNING_OF_TIME);
        cal.setTime(date);
        StringBuffer buff = new StringBuffer(12);
        int year = cal.get(Calendar.YEAR);
        if (date.getTime() < 0) {
            if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
                year = -year + 1;
            }
        }
        formatYearNumber(year, buff);
        buff.append('-');
        formatTwoDigits(cal.get(Calendar.MONTH)+1, buff);
        buff.append('-');
        formatTwoDigits(cal.get(Calendar.DAY_OF_MONTH), buff);
        return buff.toString();
    }
View Full Code Here


     *
     * @return a instance of SerialDate.
     */
    public static SerialDate createInstance(final java.util.Date date) {

        final GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return new SpreadsheetDate(calendar.get(Calendar.DATE),
                                   calendar.get(Calendar.MONTH) + 1,
                                   calendar.get(Calendar.YEAR));

    }
View Full Code Here

    {
      final int y = getIntParameter("year");
      final int m = getIntParameter("month");
      final int d = getIntParameter("day");

      return new GregorianCalendar(y, m, d).getTime();
    }
  }
View Full Code Here

{
    private Log log = LogFactory.getLog(DayFromDate.class);
   
    public Map<String, Integer> getDay(String aDate) throws Exception
    {
        Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
        Map<String, Integer> aMap = new HashMap<String, Integer>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        formatter.setCalendar(calendar);
        calendar.setTime(formatter.parse(aDate));
        log.info("Date processed is: " + calendar.getTime());
        aMap.put(aDate, calendar.get(Calendar.DAY_OF_WEEK));
        return aMap;
    }
View Full Code Here

    /* Create a table data model for the table widget and add
     * some rows to it */
    tableModel = new TableDataModel();
    tableModel.addRow(
      new Person("Wenzel", "Jakob", "wazlaf@tigris.org", new GregorianCalendar(1983, 11, 04).getTime()));
    tableModel.addRow(new Person("Hans", "Mustermann", "asdf@asdf.com", new Date()));
    tableModel.addRow(new Person("Test", "Person", "test@test.de", new Date()));

    /* Add a debugging model change listener to the table data model */
    tableModel.addModelChangeListener(new ModelChangeListener() {
View Full Code Here

    }
   
    public void setDate(Date dt) {
        if (dt != null) {
            if (calendar == null)
                calendar = new GregorianCalendar();
           
            calendar.setTime(dt);
            updateScreen(calendar);
           
            original = Calendar.getInstance();
View Full Code Here

    private void build() {
        setTitle(DcResources.getText("lblPickADate"));
       
        setModal(true);
        setResizable(false);
        calendar = new GregorianCalendar();
        navigationPanel = new NavigationPanel(this);

        /** actions */
        JPanel actionsPanel = new JPanel();
        actionsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
View Full Code Here

    }
   
    @Override
    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        Calendar c = new GregorianCalendar();
       
        c.setTime(parent.getCalendar().getTime());
       
        if (e.getSource() instanceof JButton) {
            if (e.getActionCommand().equals("prevMonth"))
                c.add(Calendar.MONTH, -1);
            if (e.getActionCommand().equals("nextMonth"))
                c.add(Calendar.MONTH, 1);
            if (e.getActionCommand().equals("prevYear"))
                c.add(Calendar.YEAR, -1);
            if (e.getActionCommand().equals("nextYear"))
                c.add(Calendar.YEAR, 1);
           
            parent.updateScreen(c);
        } else {
            if (e.getActionCommand().equals("monthChanged")) {
                JComboBox cb = (JComboBox)src;
                c.set(Calendar.MONTH, cb.getSelectedIndex());
            }
   
            if (e.getActionCommand().equals("yearChanged")) {
                JComboBox cb = (JComboBox)src;
                c.set(Calendar.YEAR, years[cb.getSelectedIndex()].intValue());
                setYearComboBox(c);
            }
            parent.setMonthPanel(c);
        }
    }
View Full Code Here

        parent = null;
        ComponentFactory.clean(this);
    }
   
    private void setDaysOfMonth(Calendar c) {
        Calendar curr = new GregorianCalendar();
        int currdate = curr.get(Calendar.DAY_OF_MONTH);
        int currmon = curr.get(Calendar.MONTH);
        int curryear = curr.get(Calendar.YEAR);

        int seldate = -1;
        int selmon = -1;
        int selyear = -1;
       
View Full Code Here

   
   
    //"Today hh:mm" and "Y-day hh:mm" cases
    if(s.startsWith("today ") || s.startsWith("y-day ")) {
      try {
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeZone(timeZone);
        String time = s.substring(6);
        StringTokenizer st = new StringTokenizer(time,":");
        int hours = Integer.parseInt(st.nextToken());
        int minutes = Integer.parseInt(st.nextToken());
        calendar.set(Calendar.HOUR_OF_DAY, hours);
        calendar.set(Calendar.MINUTE,minutes);
        if(s.startsWith("y-day ")) {
          calendar.add(Calendar.DATE, -1);
        }
        return calendar.getTime();
      } catch(Exception e) {
        e.printStackTrace();
        return null;
      }
    }
   
    //"07-25 2006", "02-01 02:53" and "03 Mar 2006" cases
    if(s.length() > 3) {
      String thirdCharacter = s.substring(2,3);
     
      //"07-25 2006" and "02-01 02:53" cases
      if(thirdCharacter.equals("-")) {
        if(s.length() > 9) {
          String ninthCharacter = s.substring(8,9);
          //"02-01 02:53" case
          if(ninthCharacter.equals(":")) {
            try {
              int month = Integer.parseInt(s.substring(0,2));
              int day = Integer.parseInt(s.substring(3,5));
              int hours = Integer.parseInt(s.substring(6,8));
              int minutes = Integer.parseInt(s.substring(9,11));
              Calendar calendar = new GregorianCalendar();
              calendar.setTimeZone(timeZone);
              calendar.set(Calendar.MONTH, month-1);
              calendar.set(Calendar.DAY_OF_MONTH,day);
              calendar.set(Calendar.HOUR_OF_DAY, hours);
              calendar.set(Calendar.MINUTE,minutes);
              return calendar.getTime();
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          //"07-25 2006"
          else {
            try {
              return MMddyyyyFormat.parse(s);
            } catch (Exception e) {
              e.printStackTrace();
           
          }
        } else {
          //"03-25" case
          try {
            int month = Integer.parseInt(s.substring(0,2));
            int day = Integer.parseInt(s.substring(3,5));
            Calendar calendar = new GregorianCalendar();
            calendar.setTimeZone(timeZone);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH,day);
            return calendar.getTime();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
       
      } else if(s.length() == 9 && s.indexOf(" ") != -1){
        //"21 Mar 08" case
        try {
          return ddMMMyyFormat.parse(s);
        } catch (Exception e) {
          if(DEBUG) {e.printStackTrace();}
        } 
      }
     
      //"03 Mar 2006" case
      if(thirdCharacter.equals(" ")) {
        try {
          return ddMMMyyyyFormat.parse(s);
        } catch (Exception e) {
          if(DEBUG) {e.printStackTrace();}
        }
      }
    }
   
    //Age based stuff
    if(    s.endsWith(" ago") ||
        s.indexOf("month")!= -1 ||
        s.indexOf("hour") != -1 ||
        s.indexOf("day") != -1 ||
        s.indexOf("week") != -1 ||
        s.indexOf("year") != -1) {
     
      s= s.replaceAll(" ago", "");
      StringTokenizer st = new StringTokenizer(s," ");
      if(st.countTokens() >= 2) {
        try {
          Calendar calendar = new GregorianCalendar();
          while(st.hasMoreTokens()) {
            float value = Float.parseFloat(st.nextToken());
            String unit = st.nextToken();
           
            calendar.setTimeZone(timeZone);
            if(unit.startsWith("min")) {
              calendar.add(Calendar.MINUTE, -(int)value);
            }
            if(unit.startsWith("hour")) {
              calendar.add(Calendar.HOUR_OF_DAY, -(int)value);
            }
            if(unit.startsWith("day")) {
              calendar.add(Calendar.DATE, -(int)value);
            }
            if(unit.startsWith("week")) {
              calendar.add(Calendar.WEEK_OF_YEAR, -(int)value);
            }
            if(unit.startsWith("month")) {
              calendar.add(Calendar.MONTH, -(int)value);
            }
            if(unit.startsWith("year")) {
              calendar.add(Calendar.YEAR, -(int)value);
            }
          }
          return calendar.getTime();
        } catch (Exception e) {
          if(DEBUG) {e.printStackTrace();}
        }
      }
    }
   
    if(s.equals("today")) {
      Calendar calendar = new GregorianCalendar();
      calendar.setTimeZone(timeZone);
      //calendar.set(Calendar.HOUR_OF_DAY,12);
      //calendar.set(Calendar.MINUTE,0);
      return calendar.getTime();
    }
   
    if(s.equals("yesterday")) {
      Calendar calendar = new GregorianCalendar();
      calendar.setTimeZone(timeZone);
      calendar.add(Calendar.DATE, -1);
      //calendar.set(Calendar.HOUR_OF_DAY,12);
      //calendar.set(Calendar.MINUTE,0);
      return calendar.getTime();
    }
   
    try {
      StringTokenizer st = new StringTokenizer(s," ");
      Calendar calendar = new GregorianCalendar();
      calendar.setTimeZone(timeZone);
      while(st.hasMoreTokens()) {
          String element = st.nextToken();
          int field = -1;
          int end_offset = -1;
          if(element.endsWith("h")) {
            field = Calendar.HOUR_OF_DAY;
            end_offset = 1;
          }
          if(element.endsWith("d")) {
            field = Calendar.DAY_OF_MONTH;
            end_offset = 1;
          }
          if(element.endsWith("w")) {
            field = Calendar.WEEK_OF_YEAR;
            end_offset = 1;
          }
          if(element.endsWith("m")) {
            field = Calendar.MONTH;
            end_offset = 1;
          }
          if(element.endsWith("mon")) {
            field = Calendar.MONTH;
            end_offset = 3;
          }
          if(element.endsWith("y")) {
            field = Calendar.YEAR;
            end_offset = 1;
          }
          if(field != -1 && end_offset != -1) {
            int value = (int) (Float.parseFloat(element.substring(0,element.length() - end_offset)));
            calendar.add(field,-value);
          }
      }
     
      return calendar.getTime();
     
    } catch (Exception e) {
      if(DEBUG) {e.printStackTrace();}
    }
   
View Full Code Here

TOP

Related Classes of java.util.GregorianCalendar

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.