Date and Calendar Classes

Well, I'm trying to do a program which valildates a Date given by the User (Does it matter if it's an example using the console, or not?? 'Cuz I'm using it on a Swing application). I WAS using
date's constructor:
new Date(y, m, d); but it doesn't do a thing, I can make a new instance
of Date EVEN if it's February 31st (WHICH IS NOT RIGHT!!!)... how can I do it??

Im not really sure if I understand what your asking, but I thnk that in the constructor you need some stuff like
if ( m > 12){
      m = 0
if ( m == 2 && d > 28 ){ // that would be just for febuary
      d = 0
}But you probobly would have to do that for every month.
hope this helps

Similar Messages

  • 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

  • Overview of Dates and Calendar

    hi,
    does anyone know some good links that explain what is going on with Date and Calendar etc..
    It seems quite complicated - is there any docs on the general idea/framework? (and why things have been deprecated etc..)
    I don't know anything about different world calendars, but theres lot on the web, so its more the java specifics I'm interested in!
    thanks,
    asjf

    Don't use Date at all, use Calendar instead (unless you need to use dates in databases). What parts of the Calendar docs aren't clear? The general idea is each instance of Calendar represents a point in time, with methods to change and retrieve various datetime fields.

  • Any Expert on Date and Calendar????

    Once I set my own calendar .. why it's not keeping my calendar and printing the date and time onwards from my set date time...
    I just print only one time the date/time but later in my thread, it prints OS date and time....?
    Any Ideas??
    import java.util.*;
    import java.text.*;
    public class DateCheck {
      DateCheck(){
        public static void main(String[] args){
            System.out.println("System  bb");
      //      DateCheck d = new DateCheck();
                   System.out.println("In constructor");
            Calendar d3 = Calendar.getInstance();
            d3.set(Calendar.YEAR,2005);
            d3.set(Calendar.MONTH,Calendar.OCTOBER);
            d3.set(Calendar.DAY_OF_MONTH,27);
            d3.set(Calendar.HOUR,01);
            d3.set(Calendar.MINUTE,01);
            d3.set(Calendar.SECOND,01);
        //    Date dt = new Date(2005,05,01,01,01);
           System.out.println("Start...")  ;
                try{
                    //System.out.println("Here:  " + dfNormal.format(now));
                   // System.out.println("There: " + dfGMT.format(new Date()));
                    System.out.println(" Five seconds Before : " + d3.getInstance().getTime());
          //          System.out.println(" Five seconds Before date : " + dt.getTime());
                    Thread.sleep(5000);
                    //  System.exit(0);
                }catch (Exception e){System.out.println("Some Problems" +e);}
            while(true){
                try{
                    //System.out.println("Here:  " + dfNormal.format(now));
                   // System.out.println("There: " + dfGMT.format(new Date()));
                    Thread.sleep(5000);
                    System.out.println("NOW : " + d3.getInstance().getTime());
            //         System.out.println(" Now date : " + dt.getTime());
                    Thread.sleep(1000);
                    //  System.exit(0);
                }catch (Exception e){}
    }

    Chuck, Thanks for you reply..
    But your solution would 'nt work if some one change the system date..
    I need some solid solution....
    If you do not understand what i am say then do this
    Run your solution.. put it in the thread.... and change your time from
    your computer... and seee what happens....
    Thanks again

  • Question on Dates and Calendar

    i need to find the number of days between 2 given dates. For example, the number of days between Nov 29th 2006and Dec 3rd 2006 is 4. The number of days between Nov 10th and Nov 15th is 5. How do i do this? Is there an api for doing this?

    No API does this; by using the Claendar and related classes it's easily calculated. See these:
    http://onesearch.sun.com/search/onesearch/index.jsp?qt=date+difference&subCat=siteforumid%3Ajava31&site=dev&dftab=siteforumid%3Ajava31&chooseCat=javaall&col=developer-forums

  • Date and Calendar

    Why is it so confusing to get the time
    can someone tell me what's the best way to get the time

    Why is it so confusing to get the time
    can someone tell me what's the best way to get the
    timefrom java.util.Calendar.getInstance you can get a Calendar object which contains current date and time.
    you can use java.text.SimpleDateFormat to get the string which formatted the date or time.

  • Ical or Calendar (or whatever) doesn't detect dates and calendar names anymore!

    Hi everyone.
    I used to have a weekly email created with automator and ICal, parsing a specific calendar by its name, and creating
    a list of events occurred in the week (monday to sunday) to send. I used an automator ical alarm for this (now called automator CALENDAR alarm???).
    It worked like a charm.
    Enter Mavericks.
    It's doesn't work anymore.
    Couldn't open the file. So I set to edit it and re-create it, but to no avail.
    I've tried to debug it, by going down to basic actions and their output, and I found out that for example:
    - A one action workflow, find calendar events, with date starting this week, doesn't work!
         No output, when I have pretty many events occurring in the week.
         It doesn't work even if I change the condition (today, or 2 days from now etc...)
    - Another single action workflow (still for testing purposes), filter calendar where title contains any calendar name returns nothing, while there are many calendars in the app!
    Can anyone help?
    Did you experience the same problems?

    Any solution on this problem? I'm experiencing the exact same problem on our 2 GHz iMac Core 2 Duo.

  • Date and Calendar object

    Hi there,
    I am trying to display the current seconds by using the following method. It keeps displaying the first value, over and over. I also tried using the date object and got the same result. Can you help me? Do you know why it does that? Thank you.
    Berk Can Celebisoy
    Calendar calendar;
    calendar = calendar.getInstance();
    public static void main(String[] args)
    //just to get in the loop
    while (2+2==4)
    try
    System.out.println(calendar.get(calendar.SECOND));
    //below line is by using the date object
    //System.out.println(date.getSeconds());
    this.sleep(1000);
    catch (InterruptedException e){}
    /////////////////////////////////////

    Thanks for your reply.
    Berk.
    public static void main(String[] args)
    Calendar calendar = null;
    //just to get in the loop
    while (true)
    calendar = Calendar.getInstance(); //get the instance
    each time u loop
    try
    System.out.println(calendar.get(calendar.SECOND));
    Thread.sleep(1000);
    catch (InterruptedException e){}

  • Where do Date and Calendar get the current date?

    The server's time is correct, but my Java application is one hour fast... Why could that be?

    Maybe because Java has a different notion of "local time zone" than whatever you use to check the time.
    Try checking the local timezone that Java thinks is in effect and compare this with whatever the OS provides.
    A common case seems to be that Java has more up-to-date timezone data than the OS and Java is actually true.
    And: you could easily make a mistake by converting Date/Calendar/... around an mess up the timezone information (don't ask me how exactly, but I've seen it before).

  • How can I get Chinese characters in the date and calendar?

    The other night I was using a friend's Japanese iBook. I noticed that she the day displayed with a Chinese character in the menu bar and in the calendar widget (e.g., Her clock said 月4:27 instead of Monday 4:27), but her interface was in English (no Japanese menus).
    How can I set the day to display with a Chinese character?
    iBook G4 12   Mac OS X (10.4.8)  

    System Preferences/International/Formats/Region/Japan

  • Financial Reporting - Derived data and calendar periods

    I have two questions with Financial Reporting
    1. I have a P&L report that shows the full 12 months. FR displays the derived data of the next actual period even if there has been no consol. It therefore looks like all the data has been reversed to zero. Is there a setting to stop FR from displaying derived data?
    2. The application has a June year end. We want to display in the report titles the months based on their calender year rather than the fiscal year. This means that for Sept we want Sept 2011 to display, not Sept 2012 that will come from the Year dimension. Is there a function to handle that?
    regards

    1. I have a P&L report that shows the full 12 months. FR displays the derived data of the next actual period even if there has been no consol. It therefore looks like all the data has been reversed to zero. Is there a setting to stop FR from displaying derived data?
    The answer for this might be the VIEW dimension.If u have taken YTD please change it to Periodic and give a try.
    2. The application has a June year end. We want to display in the report titles the months based on their calender year rather than the fiscal year. This means that for Sept we want Sept 2011 to display, not Sept 2012 that will come from the Year dimension. Is there a function to handle that?
    While creating application profile we have a choice to select the years or just look into application settings.I am not sure just give a try.

  • Applet and Calendar class

    Hi,
    I am developing a Applet, which has lot of functions which needs calendar
    So i use Calendar.getInstance(); to get the instance if calendar
    But this applet will be used by clients all over the world,
    So i want to know, what will be the effect if i use Calendar.getInstance(); to get the instance of calendar,
    Any comments, help , or documents
    Thanx
    Ashish

    The timezone that the client machine, and the datetime format and all the other localized properties will be a part of the instance.
    Hope this helps.

  • Date and currency in business apps

    Last year, I was concerned about the difficulty to use java.util.Date class in business applications.
    See thread "Date is a mess for business use" at http://forum.java.sun.com/thread.jsp?forum=4&thread=464726
    And yes, I continue to think that a Date with 61 seconds in a minute is unusable in a business environment !
    We need to store business dates, not timestamps.
    Working for an insurance company, suitable date and currency classes are of paramount importance, but Java doesn't include such classes, rendering its use very difficult, not to say almost impossible.
    I just give here a pointer to some very interesting articles I just came across lately.
    I think it can be of some help to people facing the same problems as me about Date and currency storage.
    http://www.idinews.com/darkside.pdf
    http://www.idinews.com/freeware.html
    http://www.idinews.com/sourcecode/DateJava.html
    http://www.idinews.com/sourcecode/MoneyJava.html
    At last, with these two classes, we can do the same data processing as we did with all our cobol programs.
    Happy reading !

    I tried using the Money class on the compound interest example from http://www.cs.princeton.edu/introcs/91float/. Here's my code:
    Money deposit = new Money(1000d);
    System.out.println("Deposit = "+deposit);
    System.out.println("Double precision interest = "+
         deposit.mpy(Math.pow(1 + .05 / 365, 365)));
    Money dailyAmount = new Money(deposit);
    for (int i = 0; i < 365; i++)
              dailyAmount = dailyAmount.mpy (1 + .05 / 365);
    System.out.println("Daily calculated interest = "+dailyAmount);The output looks like:
    Deposit = $1,000.00
    Double precision interest = $1,051.26
    Daily calculated interest = $1,049.40But the second value should be $1,051.27. Because Money uses a long to represent the amount I end up missing a penny when double precision rounding is applied, and because it rounds down I end up missing almost $2 when the amount is calculated daily.

  • I am using Calendar class, and want to see if a date is older then 24 hours

    i have a Calendar class instance, and i want to test if the current date & time is older then 24 hours. i have been playing with for 2 hours. anyone know how to do this?

    One way is to call .getTime().getTime() (yes, that's 2 of 'em) on the Calendar object, which converts the Calendar's time to a milliseconds value which can be compared to System.currentTimeMillis()
    The two times are within 24 h of each other if the ms values differ by less than 86 400 000.

  • Why not Deprecate java.util.Date and java.util.Calendar

    With the introduction of java.time, why did you not flag java.util.Date and java.util.Calendar. These classes have been a bane to every Java developer and should never be used again with the introduction of Java 1.8.

    Adding the @Deprecated annotation would only just provide a warning about an old API and recommendation to the developer(s) to no longer use it. Doing so would not break any existing library out there; in fact quite a number of constructors and methods on the Date class have already been flagged deprecated.
    The new java.time package is far superior to Date/Calendar.

Maybe you are looking for

  • Best Practice

    What is the best practice with respect to cursor use in Oracle?

  • J2ME J2EE(servlet jsp) chat server

    want to make a chat server using J2ME(fronend) and J2EE(server) . Could anybdy tell me some sample applications on the net or tutorials that can help

  • Sub Contracting for Production Order

    Hi All, Scenario is like this, I release Production order & deicde to sent it to sub contracting & PO Created is wth Acct assignment F with no material master. after this how do i account sub-contacting bills with MIGO i'm not able to make a GR. shou

  • Business Explorer Analyzer not installed

    Hi, I installed SAP GUI 710 (patch level 8) and I have no problems going into Bex Query Designer but when I try clicking for Analyzer, it doesnt work. Nothing happens.. But when I try using transation RRMX, i get an error that the Business Explorer A

  • How to understand func spec

    Can u tell me the better way of understanding and analyzing the functional specification