Add date on Calendar

I try to create the code below. today is 7/2/07, but when I -2 days it shows 6/31/07. but there is no such a date. what do I do to make it go to 6/30/07?
Calendar now = Calendar.getInstance();
Calendar calendar;
calendar = (Calendar) now.clone();
//date of calendar is 6/2/07
calendar.add(Calendar.MONTH, 1);
//date of calendar is 7/2/07
calendar.add(Calendar.DAY_OF_YEAR, -2);
//date of calendar is 6/31/07

I had to change EST to PST time and was not able to find method to do that so I created one dateAdd().
To add date you need to pass date as string in the format yyyyMMddHHmmss, string date part such as y, M, d, h, m, or s and positive or negative integer to get future or past date. You can change date format in the code and then you will pass according date string, or add parameter to specify format for your date. Ok, you got the idea.
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.*;
private String dateAdd(String sDate, String sPart, int i){
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
Date d1 = new Date ();
String sStr = "";
if (StringUtils.isNumeric(sDate)){
if (sDate.length()>13){
System.out.println("sD = " + sDate);
sStr = sDate.substring(14);
sDate= sDate.substring(0,14);
}else{
sDate = StringUtils.rightPad(sDate, 14, "0");
try{
d1 = df.parse(sDate);
Calendar cal = Calendar.getInstance();
cal.setTime(d1);
if(sPart.equals("s")){
cal.add(Calendar.SECOND, i);
else if(sPart.equals("m")){
cal.add(Calendar.MINUTE, i);
else if(sPart.equals("h")){
cal.add(Calendar.HOUR, i);
else if (sPart.equals("d")){
cal.add(Calendar.DAY_OF_MONTH, i);
else if(sPart.equals("M")){
cal.add(Calendar.MONTH, i);
else if(sPart.equals("y")){
cal.add(Calendar.YEAR, i);
sDate = df.format(cal.getTime())+ sStr;
} catch (Exception ex) {
ex.printStackTrace();
return sDate;
Enjoy dateAdding.
Thank you.

Similar Messages

  • I can't add any new calendar or date in iCal (iPad, iOS5)

    I can't add any new calendar or date in iCal since running iOS5. The corresponding buttons doesn't appear on the interface. I have restore one time and it changes anything.
    Thanks for help.

    Assuming you activated iCloud, try going to Settings>iCloud and make sure Calendars and Contacts are set to ON.

  • Iphone 4s , i cannot add dates to my calendar

    i recently updated my system ios software and now i cannot add dates to my calender.  i've powering down and back up again.

    Tap the big + sign at the top right of any date.

  • Date to Calendar Conversion

    I want to convert following class file from Date to Calendar
    pls guide me how shall i go about it
    package com.utility;
    import java.util.Date;
    public class ConstDate extends Date {
         public ConstDate(Date d)
            super(d.getTime());
        public ConstDate(long time)
            super(time);
        public void setTime(long time)
            throw new UnsupportedOperationException("Can not set time on ConstDate");
        public static ConstDate make(Date date)
            return (date instanceof ConstDate) ? (ConstDate)date : new ConstDate(date);
    }when i tried to do it said i need to implement the abstract methods, i want to know how can i go about doing the same too
    thanks in advance
    regards
    Prad

    Great, but the demand is to change from date to calendar so how can i extend it from interface i have made something like this
    import java.util.Calendar;
    public interface AbstractConstCalendar{
         public abstract Calendar add(int i, int j);
        public abstract Calendar computeFields();
        public abstract Calendar computeTime();
    Note: i just changed the above class file name from ConstDate to ConstCalendar
    how can i call this in above ConstCalendar Class, will this help
    regards
    prad

  • I would like to mark data on my iPhone / iPad as private or hide it. Is it possible? I could do it on my Palmpilot. Any data - addresses, calendar entries, files, anything could be hidden on the palm pilot. It'd be great to see this facility on apl pdts

    I would like to mark data on my iPhone / iPad as private or hide it and only unlock it with a different password. Is it possible? I could do it on my Palmpilot. Any data - addresses, calendar entries, files, anything could be hidden on the palm pilot. It'd be great to see this facility on apple products.

    I know what you meant. As I said, it's not possible to do so on the iPhone. Of course Apple could add such a feature to a future version of iOS, it's only software, but so far they haven't. You can suggest such a feature to Apple here: http://www.apple.com/feedback/iphone.html

  • I can no longer add to my calendar on my iPad but it appears on my phone only.

    I can no longer add to my calendar on my iPad but it appears on my phone only.

    Try reset iPad
    Hold down the Sleep/Wake button and the Home button at the same time for at least ten seconds, until the Apple logo appears
    Note: Data will not be affected.

  • How to implement a Date Picker (Calendar) as a LOV in Portal Form?

    I have a form, which will input date from user, and I also have javascript calender.
    How to implement a Date Picker (Calendar) as a LOV in Portal Form? Do I need to choose POPUP and LOV for Date?
    Instead of typing the Date within a Portal form, a popup Calender that will allow user to Pick a date.
    Please help anyone!!

    Once you have the JavaScript and have added it to your form, you need to the the following. First off, you need to be able to customize the HTML code for the form (i.e. choose custom for form type when creating the form). Once you do that go into the custom layout to customize your HTML code. Find the field you want to add the date picker to. It will be <#your_field_name.ITEM#>
    Paste this code after it:
    <a href="javascript:show_calendar('forms[0.elements[16');" onmouseover="window.status='Date Picker';return true;" onmouseout="window.status='';return true;"><img src="the source of your image file" width=34 height=21 border=0></a></TD>
    You will have to play around with the forms[0].elements[16]. 16 is what date field is set to. However your will differ, so just play with it.
    Hope this helps.
    Martin

  • Automatically add Date events from Microsoft Word,MS Outlook to ical?

    Hi
    Was wondering if theres anyway(native or software) for outlook,word or ical to automatically scan and add date events from docs,mail to ical so that i can sync it to my iphone

    I am anxiously awaiting delivery of my new 24 inch iMac and I've got tons of contacts and calendar data to export from MS Outlook in PC to my new Mac. I also got loads of emails to export from MS Outlook Express to Mac Mail which I'm beginning to fear.
    Would be a real relief if someone can provide some hints or ideas. Thanks a million.

  • HOW TO VIEW & EDIT/ADD DATA TO ICAL FROM WINDOWS XP

    I am new to macs & have been using windows xp at my office. I want to know whether there is a way to edit/add my ical calendars & to-do lists from windows xp. I tried using mozilla calendar but it is pathethic. ical data is not compatible with outlook 2003 & entourage for mac os x is also not comaptible with outlook even when both are made by microsoft. Now I'm stuck with either ical or outlook or entourage . I know that entourage data can be imported in ical but I wanna edit/add the calendars/to do list on a windows pc. please help

    I would also like to know if this is possible.
    I manage a band and currently we use a bravenet web calendar to keep track of gigs and people's availability. Band members can access the calendar via a web browser and update their availability.
    They are windows users and I use a mac. I know that they can access my ical calendar as read only but I would like for them to be able to add stuff to a calendar that is sync'd in some way to my ical calendar so I don't need to update both the bravent calendar and my ical calendar.
    It would be great if someone could help with this please
    Thanks,
    Mike

  • Option to add invitees in Calendar app gone

    My phone is up to date with iOS 6.0.1, but ever since I updated to iOS 6, the option to add invitees to calendar events has been gone. What changed and how do I get that back???

    Hi Friend,
    Your event should be sync with some iCloud Calendars.
    Create an event. Then tap the event in Calendars. Then tap Edit, then tap Invitees to select people from contacts.

  • TableCellEditor that Supports Both Date and Calendar

    Hi,
    I'm trying to write a TableCellEditor that supports both the Date and Calendar Classes (i.e., my table could have either Dates or Calendar objects, and I want this cell editor to work seamlessly with either one).
    Here is a simplified version of my code:
    public class DateCellEditor extends AbstractCellEditor implements TableCellEditor {
    /** The editor component. */
    private DateChooser editor;
    private Calendar cal;
    public DateCellEditor() {
    super();
    editor = new DateChooser();
    cal = Calendar.getInstance();
    public Object getCellEditorValue() {
    cal.setTime(editor.getDateValue());
    return cal;
    public Component getTableCellEditorComponent (JTable table,
                                                                                                                            Object value,
                                                                                                                            boolean isSelected,
                                                                                                                            int row,
                                                                                                                            int column) {
    if (value != null) {
    if (value instanceof Calendar)
         editor.setDateValue(((Calendar)value).getTime());
         else if (value instanceof Date)
         editor.setDateValue((Date)value);
    return editor;
    As you can see, I've already modified getTableCellEditorComponent to accepted object of class Date or Calendar. My problem arises in the getCellEditorValue method. How do I know which type of object to return? In the code sample above, I'm returning a Calendar object, but I'm not sure how to code this method to return either Calendar OR Date.
    By the way, editor.getDateValue() returns a Date object.
    I could easily write two separate TableCellEditors, but I would prefer a more elegant solution. Any ideas?
    Thanks!
    Karen Prengaman

    If you really must have both objects and don't want to convert one into another, you could add a function like
    setMode(int modetype) {}
    where modetype is either DATE or CALENDAR, and then return the set type when the user calls getValueAt

  • Pls Help me with steps to add data from xml file to SAP B1 through B1iSN.

    Pls Help me with steps to add data from xml file to SAP B1 through B1iSN. I  am getting stuck in xsl transformation. not able to understand where the mapping code needs to be added.
    Pls explain me the steps for adding data from xml to B1 quotation step by step.
    thanks and regards
    Priya

    Hi,
    Have you checked this: https://sap.na.pgiconnect.com/p45508295/?launcher=false&fcsContent=true&pbMode=normal ?
    Thanks,
    Gordon

  • Add data to the table in the database with the use of add button

    The name of my database is Socrates.
    The name of the table in the database is Employees
    I want to be able to add data to the database. i am presently working on the add button such that when i enter date into the textfield and press the add button it should automatically register in the table.
    The error upon compilation is with this line of code
    If (ae.getSource() == jbtnA)// it says that ";" is expected
    Below is the entire code
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Mainpage extends JFrame implements ActionListener
         JTextField jFirstName = new JTextField(15);
         JTextField jSurname = new JTextField(12);
         JTextField jCity = new JTextField(10);
         JTextField jCountry = new JTextField(12);
         JTextField jSSN = new JTextField(8);
         JLabel jFirstLab = new JLabel("First Name");
         JLabel jSurnameLab = new JLabel("Surname");
         JLabel jCityLab = new JLabel("City");
         JLabel jCountryLab = new JLabel("Country");
         JLabel jSSNLab = new JLabel("Social Security Number (SSN)");
         JButton jbtnA = new JButton ("Add");
         JButton jbtnPrv = new JButton ("Previous");
         JButton jbtnNt = new JButton ("Next");
         JButton jbtnDl= new JButton ("Delete");
         JButton jbtnSrch = new JButton ("Search");
         public Mainpage (String title)
              super (title);
              Container cont = getContentPane();
              JPanel pane1 = new JPanel();
              JPanel pane2 = new JPanel();
              JPanel pane3 = new JPanel();
              pane1.setLayout (new GridLayout (0,1));
              pane2.setLayout (new GridLayout(0,1));
              pane3.setLayout (new FlowLayout());
              pane1.add(jFirstLab);
              pane1.add(jSurnameLab);     
              pane1.add(jCityLab);
              pane1.add(jCountryLab);
              pane1.add(jSSNLab);
              pane2.add(jFirstName);
              pane2.add(jSurname);
              pane2.add(jCity);
              pane2.add(jCountry);
              pane2.add(jSSN);
              pane3.add(jbtnA);
              pane3.add(jbtnPrv);
              pane3.add(jbtnNt);
              pane3.add(jbtnDl);
              pane3.add(jbtnSrch);
              cont.add(pane1, BorderLayout.CENTER);
              cont.add(pane2, BorderLayout.LINE_END);
              cont.add(pane3, BorderLayout.SOUTH);
              jFirstName.addActionListener(this);
              jSurname.addActionListener(this);
              jCity.addActionListener(this);
              jCountry.addActionListener(this);
              jSSN.addActionListener(this);
              jbtnA.addActionListener(this);
              jbtnPrv.addActionListener(this);
              jbtnNt.addActionListener(this);
              jbtnDl.addActionListener(this);
              jbtnSrch.addActionListener(this);
              validate();
              setVisible(true);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              pack();
              setResizable(false);
         public void actionPerformed(ActionEvent ae)
                   If (ae.getSource() == jbtnA)
                                    fst = jFirstName.getText();
                        srn = jSurname.getText();
                        cty = jCity.getText();
                        cnty = jCountry.getText();
                        int sn =
    Interger.parseInt(jSSN.getText());
                                    String ad = "Insert into Employees
    (Firstname,Surname,City,Country,SSN)" +
    "values('"fst"','"srn"','"cty"','"cnty"','"sn"')";
                        Statement stmt = con.createStatment();
                        int rowcount = stmt.executeUpdate(ad);
                        JOptionPane.showMessageDialog("Your
    details have been registered");
                        Statement stmt = con.createStatment();
                        int rowcount = stmt.executeUpdate(ad);
    public static void main (String args[])
              Mainpage ObjFr = new Mainpage("Please fill this
    registration form");
              try
                   Class.forname("sun.jdbc.odbc.JdbcOdbcDriver");
                   String plato = "jdbc:odbc:socrates";
                   Connection con =
    DriverManager.getConnection(plato);
              catch(SQLException ce)
                   System.out.println(ce);
    }

    i have restructured the code, but the following line of code is giving error:
    String plato = jdbc:odbc:socrates;
    the entire code is below:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.sql.*;
    public class Mainpage extends JFrame implements ActionListener
         JTextField jFirstName = new JTextField(15);
         JTextField jSurname = new JTextField(12);
         JTextField jCity = new JTextField(10);
         JTextField jCountry = new JTextField(12);
         JTextField jSSN = new JTextField(8);
         JLabel jFirstLab = new JLabel("First Name");
         JLabel jSurnameLab = new JLabel("Surname");
         JLabel jCityLab = new JLabel("City");
         JLabel jCountryLab = new JLabel("Country");
         JLabel jSSNLab = new JLabel("Social Security Number (SSN)");
         JButton jbtnA = new JButton ("Add");
         JButton jbtnPrv = new JButton ("Previous");
         JButton jbtnNt = new JButton ("Next");
         JButton jbtnDl= new JButton ("Delete");
         JButton jbtnSrch = new JButton ("Search");
         Statement stmt;
            String ad;
            public Mainpage (String title)
              super (title);
              Container cont = getContentPane();
              JPanel pane1 = new JPanel();
              JPanel pane2 = new JPanel();
              JPanel pane3 = new JPanel();
              pane1.setLayout (new GridLayout (0,1));
              pane2.setLayout (new GridLayout(0,1));
              pane3.setLayout (new FlowLayout());
              pane1.add(jFirstLab);
              pane1.add(jSurnameLab);     
              pane1.add(jCityLab);
              pane1.add(jCountryLab);
              pane1.add(jSSNLab);
              pane2.add(jFirstName);
              pane2.add(jSurname);
              pane2.add(jCity);
              pane2.add(jCountry);
              pane2.add(jSSN);
              pane3.add(jbtnA);
              pane3.add(jbtnPrv);
              pane3.add(jbtnNt);
              pane3.add(jbtnDl);
              pane3.add(jbtnSrch);
              cont.add(pane1, BorderLayout.CENTER);
              cont.add(pane2, BorderLayout.LINE_END);
              cont.add(pane3, BorderLayout.SOUTH);
              jFirstName.addActionListener(this);
              jSurname.addActionListener(this);
              jCity.addActionListener(this);
              jCountry.addActionListener(this);
              jSSN.addActionListener(this);
              jbtnA.addActionListener(this);
              jbtnPrv.addActionListener(this);
              jbtnNt.addActionListener(this);
              jbtnDl.addActionListener(this);
              jbtnSrch.addActionListener(this);
              validate();
              setVisible(true);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              pack();
              setResizable(false);
              try
                   Class.forname(sun.jdbc.odbc.JdbcOdbcDriver);
                   String plato = jdbc:odbc:socrates;
                   Connection con = DriverManager.getConnection(plato);
                   stmt = con.createStatment();
              catch(SQLException ce)
                   System.out.println(ce);
              catch(ClassNotFoundException ce)
                   System.out.println(ce);
         public void actionPerformed(ActionEvent ae)
                   try
                        if(ae.getSource().equals(jbtnA))
                                         fst = jFirstName.getText();
                             srn = jSurname.getText();
                             cty = jCity.getText();
                             cnty = jCountry.getText();
                             int sn = Interger.parseInt(jSSN.getText());
                                         ad = "Insert into Employees
    values('"+fst+"',"+srn+"','"+cty+"','"+cnty+"','"+sn+"')";
                             stmt.executeUpdate(ad);
                             JOptionPane.showMessageDialog(this, "Your details have been
    registered");
                   catch(SQLException ce)
                        System.out.println(ce);
    public static void main(String args[])
              Mainpage ObjFr = new Mainpage("Please fill this registration form");
    }

  • Need to convert  Date from calendar to String in the format dd-mom-yyyy

    Need to convert Date from calendar to String in the format dd-mom-yyyy+..
    This is absolutely necessary... any help plz..
    Rgds
    Arwinder

    Look up the SimpleDateFormat class: http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
    Arwinder wrote:
    This is absolutely necessary... any help plz..For you maybe, not others. Please refrain from trying to urge others to answer your queries. They'll do it at their own pace ( if at all ).
    People on the forum help others voluntarily, it's not their job.
    Help them help you.
    Learn how to ask questions first: http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
    (Yes I know it's on JavaRanch but I think it applies everywhere)
    ----------------------------------------------------------------

  • How do you add a google calendar to muse web site, that updates are synced

    how do you add a google calendar to muse web site, that updates are synced?

    You can add Google Calender using iframe which Google provides if you go to calender settings.
    Once you insert this code in Muse page through Object > Insert HTML , the calender will be displayed on page.
    https://support.google.com/calendar/answer/41207?hl=en
    Thanks,
    Sanjit

Maybe you are looking for

  • Error with creating a new employee - PA40

    When creating a new employee record using PA40, the PERNR gets generated as soon as Infotype 0000 is updated. How do we capture the new PERNR, if a BDC recording of PA40 fails midway during the process I would appreciate and reward any valuable ideas

  • OSX Won't Install / Kernel Panic at Boot Up

    I have been having a few issues for the best part of a year now, firstly not being able to update to 10.6.8 meaning not being able to update to Lion. My onboard sound is non existemnt and have to run audio through HDMI. To cut a long story short I wa

  • Trying to share movie with "Expert Settings" - nothing happens (help?)

    When trying to "share" (export) my movie, if I select the "Expert Settings" option, nothing happens. That is, if I select "Expert Settings" and click "Share", then a dialogue window displays briefly and disappears, and the main iMovie window returns.

  • Bookmarks from original HDD

    Hi I have recently installed a new HDD with a refreshed windows OS (7). I have searched for how I can copy my bookmarks to my new profile, however the old one does not appear to list the bookmarks I had saved, so I wasnt confident in copying anything

  • KM Document iView Vs URL iView

    Hi,   We have uploaded the content(publishing html pages and documents) to the KM folders. Now we have to proceed further by creating iViews.   We are in a confusion to select type of iView<b>(either KM Document iView or URL iView)</b> to create, whi