Utilising calendar class

Hi all,
I am trying to retrieve a file with lowest timestamp from a set of files. The timestamp is contained in the filename.The following code gives the method Im using:
public String sortFilesByTimeStamp(Vector vec)
        String currFileName, selectedFile=null;
        int year,month,date,hours,mins;
        Calendar givendate=Calendar.getInstance();
        Calendar currdate=Calendar.getInstance();
        System.out.println("Currdate:"+ currdate.getTime().toString());
        if(vec==null)
            return selectedFile;
        for(int i=0;i<vec.size();i++)
           currFileName=(String)vec.get(i);
           if(currFileName!=null)
           year= Integer.parseInt(currFileName.substring(4,6));
           month=Integer.parseInt(currFileName.substring(6,8))-1;
           date=Integer.parseInt(currFileName.substring(8,10));
           hours=Integer.parseInt(currFileName.substring(10,12));
           mins=Integer.parseInt(currFileName.substring(12,14));
           givendate.set(year,month,date,hours,mins);
           System.out.println("GivenDate:"+givendate.getTime().toString());
           if((givendate).before(currdate))
               currdate=givendate;
               selectedFile=currFileName;
        return selectedFile;
    }  The vector passed to the method contains an array of strings.
However, when I run this, with the following values:
ABCD0709100600
ABCD0709100800
ABCD0709090700
the file returned is ABCD0709100600 when it has to be ABCD0709090700
am I doing something wrong here?
or is there any better approach to do this?
thanks in advance!

Aren't the strings already sorted in the correct order? Then why bother converting them to Calendar objects to do the conversion? And also, why write your own code to find the first? I would do this:Collections.sort(vec);
return (String) vec.get(0);

Similar Messages

  • How to create a Calendar Class in Universe.

    I want to create a Calendar class which will further contain Year,Quarter,Month,Day,Week Objects which can be used at report level.
    In our database we have different dates in " DD/MM/YYYY:hh:min:ss " format.
    I would like to know if this can be used to create a new Calendar Object or do I need a different table in DB like Calendar look up table provided in sample efashion universe?
    Help appreciated.

    Hi,
    Sorry for the delay in coming back - I had no access yesterday.
    I am not sure what you mean. If you have a lookup table, then you create views/aliases of this table for each of the dates you want to use it for.
    So f your transaction record contains original date and paid_date
    you would create a
    original_date_view as select * from calendar_lookup
    paid_date_view as select * from calendar_lookup
    these views could then be joined to the orginal_date and paid_date fields in your transaction table.
    In the universe you would then expose 2 classes, which contained the same objects.
    Original_date
    Date,
    Day,
    Month,
    Year
    etc
    Paid_date
    Date,
    Day,
    Month,
    Year
    etc
    If you are not using the lookup then you would have to code each date object based on the transaction date field
    So Day in orignal_date would be to_char(txn.original_date, "dd")
    while Day in Paid_date would be to_char(txn.paid_date, "dd")
    Hope this helps
    Alan

  • Problem vi� Calendar class

    Hi.
    I have a class in my project that gets the current date and time using the Calendar class. But there is a little problem. The month seems to be one month late.....now it should be 6 (June) but it displays 5 as if it is May. A similar thing happens with the hour. It is always one hour ahead of the time eg. if it is 18:54 then the time says 19:54.....does someone know what the problem is????

    Read the javadocs about the java.util.Calendar and/or java.util.Date class. Months are 0-based, not 1-based, so 0==January.

  • Discussing Calendar class

    I had a quickie utility program to write for a friend of mine and seeing as how I am incredibly new to Java I came here to look for answers. Doing a search on adding to days in an object of the Calendar class, I've made a loose obersvation based on answers.
    What I needed to do for my program was to simply increment the days. The answers I found on the forum were to use Calendar.add(Calendar.DATE, 1). However, the issue I found was the month was not being incremented and since this was a quick and dirty little utility, i didn't want to have to write a whole bunch of other code to check the date in order to increment the month.
    Or am I missing something?
    A note on the program I wrote. It was a simple command line program and the first argument was the beginning date, and the second argument was the ending date in the format of "mm/dd/yy". Also keep in mind there is other processing and code, I'm only posting what I did with the object of Calender class.
    What I did with my program was the following:
    ********** Begin Code Snippet **********
    Date bdate = new Date(Date.parse(args[0]));
    Date edate = new Date(Date.parse(args[1]));
    Date cdate = new Date(Date.parse(args[0]));
    Calendar begindate = Calendar.getInstance();
    begindate.setTime(bdate);
    Calendar enddate = Calendar.getInstance();
    enddate.setTime(edate);
    Calendar currdate = Calendar.getInstance();
    currdate.setTime(cdate); // currdate was init to begin date and will be the variable incremented
    /* other processing goes here */
    numdays = enddate.get(Calendar.DAY_OF_YEAR) - begindate.get(Calendar.DAY_OF_YEAR);     
    for (int i = 0; i <= numdays; i++)
         currdate.add(Calendar.DAY_OF_YEAR, 1);
         month = currdate.get(Calendar.MONTH) + 1;
         day = currdate.get(Calendar.DAY_OF_MONTH);
         year = currdate.get(Calendar.YEAR);
         dateString = month + "/" + day + "/" + year;
         System.out.println(dateString);
    ********** End Code Snippet **********
    Now, this works in that when I print out the dateString, the month is incremented and the day of the month gets reset to the first day when a new month is started.
    The purpose of this post is to discuss why this is wrong. Now, for the purposes of the little utility program I wrote, this is more than sufficient because all dates concerned were within the same year. (07/09/01 to 12/31/01 to be exact.)
    However, I decided to play around with this and enter dates that would span the New Year. This did not work because the variable "numdays" ends up being in the negative and the for loop does not process properly.
    Other than that issue, why is it wrong to increment the Calendar.DAY_OF_YEAR? Why is it recommended that the Calendar.DATE field is incremented? And what other processing must be done to get the month to roll over into the next month when the date changes from the 30th (or 31st or 28th, or 29th for leap years) to the 1st?
    -- Jayne

    Hi !
    Just wanted to say few things. First Calendar class's add method is an abstract method
    only (check API specification). Which means its not a concrete method and does NOTHING. You should instead use GregorianCalendar's add method . Secondly months and years do get incremented. You have to be careful because months start from 0 to 11 and not from 1 to 12.
    Here is a piece of code(a method) that adds 7 days to a dateString, you can try out :
         public void getDate(String dateStr)
              GregorianCalendar dateNextWeek = new GregorianCalendar();
              SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");     
              try {
                   dateNextWeek.setTime(formatter.parse(dateStr));          
              } catch (Exception ex) {
                   ex.printStackTrace();
              int day = dateNextWeek.get(Calendar.DAY_OF_MONTH);          
              dateNextWeek.add(Calendar.DATE, 7); //adds 7 days
              System.out.println("day = "
    + dateNextWeek.get(Calendar.DAY_OF_MONTH));
    //month begins from 0
              System.out.println("Month = " + dateNextWeek.get(Calendar.MONTH) + 1);
              System.out.println("Year = " + dateNextWeek.get(Calendar.YEAR));                    
    Hope it helps u !
    regards
    Prasanna

  • Problem with Calendar Class... Help please

    I have a simple program that use a Calendar class to know the week of year from a Date.
    Calendar cal = Calendar.getInstance();
    cal.set(2001,5,15);
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.setMinimalDaysInFirstWeek(7);
    System.out.println("week of year = "+cal.get(Calendar.WEEK_OF_YEAR));
    The problem is that in Windows OS the program work good but when I run the program on UNIX
    week_of year show Zero. The same for DAY_OF_YEAR and WEEK_OF_MONTH

    So the difference is actually the Java version, not the operating system, right? I'm not surprised that a version as old as 1.1.3 has bugs in Calendar. Upgrade.

  • 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.

  • Using Calendar class

    Hi all,
    can anybody help me for developing a program using Calendar class.what i am trying to do is
    I have javascript calendar file. daily working hours in perticular project (user give input).
    when i am selecting whole week days can i get total of weekly working hours and total in the next page or same page.
    please help me. I am strucking here
    thanks&regards
    Vijaya

    Do you want a Java program, or a JavaScript program. Java != JavaScript

  • Calendar class reuse on mutiple fields

    I have a form based on a table. There are two date fields on this form. I have followed the instructions in the online help to reuse the calendar class. The last statement in the help file tells me to create a key-listval trigger for each field that I would like to associate with the calendar class. I call the Date_LOV.Get_date() function in the two key-listval triggers. My problem is that when I run the form and click on one of the date fields, the calendar pops up correctly and places the return value in the correct date field. But if I then go to the other date field, and press F9, the trigger fires (I put an alert in both triggers to prove this) but the calendar does not appear. If I close the form , re-run it, and navigate to the other date field first, the calendar appears correctly, but when I go to the first date field, the trigger fires but the calendar does not appear. When I have the form in enter query mode, the calendar never works. Similarly, when I type other fields first and then go to the date fields, neither calendar works. The enter query mode seeting on the key-listval triggers are set to yes. How can I make this calendar work correctly for both fields?

    According to the API docs for Calendar:
    "set(f, value) changes field f to value. In addition, it sets an internal member variable to indicate that field f has been changed. Although field f is changed immediately, the calendar's milliseconds is not recomputed until the next call to get(), getTime(), or getTimeInMillis() is made."

  • Please help me for Calendar class

    Now i am working in SAP.Now we are doing Internal project for Company Timesheet of the employee,they are using Java,javascript,html,jsp,mysql.
    Please give Support for developing this task
    I am trying to do employee's timesheet( i.e. daily working hours).Here i attach my documents.here in that screen i am selecting fig1 calendar date for 19th july 2006 now we can see it in fig2 there we will see 15th july 2006 to 21st july 2006 is visible. The scnario is when ever we select the any of the day of the week we need to display whole week sun,mon to sat.
    when i am selecting (select week days by using javascript) total weekly working hours should come.please guide me and give me sample code.
    I think you understand what i am asking. using javascript calendar i am selecting the date.and from database i have to get the daily working hours of the perticular employee. I am not so familiar in java,that's why only i am asking .Please help me
    Advance thanks for You
    code i am pasting here
    thanks&Regards
    Vijaya
    ********** Java code******************
    import java.util.Calendar;
    import java.util.Date;
    class FirstAndLastDay4
    public static void main(String[] args)
    //int i=1;
    try{
    Calendar date=Calendar.getInstance();
    int day = date.get(Calendar.DAY_OF_WEEK);
    switch(day){
    case(1):
    { System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK));
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    break;
    case(2):
    { System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    break;
    case(3):
    { System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-2);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    break;
    case(4):
    { System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-3);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    break;
    case(5):
    { System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-4);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    /*for(int i=0,j=1;i<7;i++)
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+i);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    break;
    case(6):
    { System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-5);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+6);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    break;
    case(7):
    { System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-6);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+6);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+1);
    System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
    break;
    }catch(Exception e){
    e.printStackTrace();
    }

    Hi All,
    I am trying to do employee's timesheet( i.e. daily working hours).Here i attach my documents.here in that screen i am selecting fig1 calendar date for 19th july 2006 now we can see it in fig2 there we will see 15th july 2006 to 21st july 2006 is visible. The scnario is when ever we select the any of the day of the week we need to display whole week sun,mon to sat.
    when i am selecting (select week days by using javascript) total weekly working hours should come.please guide me and give me sample code.
    Thanks in advance
    thanks & regards
    <code>
    import java.util.Calendar;
    import java.util.Date;
    class FirstAndLastDay
         public static void main(String[] args)
              try{
                   Calendar date=Calendar.getInstance();
                   int day = date.get(Calendar.DAY_OF_WEEK);
                   switch(day){
                        case(1):
                        {     System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK));
                             System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+6);                         
                             System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             break;
                        case(2):
                        {     System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-1);
                             System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+6);                         
                             System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             break;
                        case(3):
                        {     System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-2);
                             System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+6);                         
                             System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             break;
                        case(4):
                        {     System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-3);
                             System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+6);                         
                             System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             break;
                        case(5):
                        {     System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-4);
                             System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+6);                         
                             System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             break;
                        case(6):
                        {     System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-5);
                             System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+6);                         
                             System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             break;
                        case(7):
                        {     System.out.println("\n Input date [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)-6);
                             System.out.println("First day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             date.set(Calendar.DAY_OF_WEEK, date.get(Calendar.DAY_OF_WEEK)+6);                         
                             System.out.println("Last day [" + new java.util.Date(date.getTimeInMillis()) + "]");
                             break;
              }catch(Exception e){
                   e.printStackTrace();
    </code>

  • Need help with Calendar class

    I am writing a class that does the following:
    Uses JComboBoxes so that the user can select Day, Month, and Year. I have the months set up January through December. When I choose a month, I wrote a subclass that gets the chosen month and enters the correct number of days in the Day ComboBox.
    Here is some sample code:
    // This code is in the main class
    monthList.addActionListener(new SelectedMonth());
    // This code is in the SelectedMonth subclass
    selected = monthList.getSelectedIndex();
    switch(selected) {
    case 0: // for January
    dayList.removeAllItems();
    g.set(Calendar.MONTH, selected);
    max = g.getActualMaximum(Calendar.DAY_OF_MONTH);
    for(int i = 1; i <= max; i++) {
    dayList.addItem(new Integer(i));
    This code is repeated for Cases 1 through 11 to cover the other months.
    After Day, Month, and Year are selected, I click an OK button and then want to send the chosen Day, Month, and Year to a database. So, I have another subclass which uses ActionListener and gets the data from the Month, Day, and Year ComboBoxes.
    However, I get runtime errors when retrieving the chosen Day using the following command:
    String fromDay = (String)dayList.getSelectedItem();
    Because the Days were put put into the Day JComboBox in a subclass, this doesn't want to work.
    Does anybody have a solution? Any help would be greatly appreciated.
    If you would like to look at all of my code, leave your e-mail address and I'll let you have a look.

    Hi,
    i got solution for ur problem, here u copy the following code and paste in some editor and try to run to see the results.
    import java.io.*;
    import java.awt.*;
    import java.util.Calendar;
    import java.awt.event.*;
    import javax.swing.*;
    public class CalExample extends JFrame
         private String months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
         private     JComboBox monthList=new JComboBox(months);
         private     JComboBox dayList=new JComboBox();
         private JButton button=new JButton();
         private int selectedMonth=1;
         private int selectedDay=1;
         public static void main(String[] args)
                        CalExample pe=new CalExample();
         public CalExample()
              JPanel panel=new JPanel();
              panel.setBackground(Color.blue);
              panel.setLayout(null);
              // Month Combo Box related init Stuff
              monthList.setBounds(100,30,100,20);
              monthList.setBackground(Color.white);
              // Day Combo Box related init Stuff
              dayList.setBounds(220,30,100,20);
              dayList.setBackground(Color.white);
              // Output textbox init stuff
              button.setBounds(340,30,100,20);
              button.setText(" Click Me ");
              panel.add(monthList);
              panel.add(dayList);
              panel.add(button);
              getContentPane().add(panel);
              setSize(500,300);
              setVisible(true);
              // Adding Listeners
              monthList.addActionListener
                        new ActionListener()
                                  public void actionPerformed(ActionEvent ae)
                                            selectedMonth=monthList.getSelectedIndex();
                                            showDays(selectedMonth);
              button.addActionListener
                        new ActionListener()
                                  public void actionPerformed(ActionEvent ae)
                                            selectedDay=dayList.getSelectedIndex();
                                       JOptionPane.showMessageDialog(null,"You Selected Month : "+(months[selectedMonth])+",--> Day : "+(selectedDay+1));
              addWindowListener(new WindowAdapter()
                   public void windowClosing(WindowEvent we)
                        System.out.println( " Exiting ...");
                        System.exit(0);
              fillCurrentDate();
         public void showDays(int selectedMonth)
              Calendar cal=Calendar.getInstance();
              cal.set(Calendar.MONTH,selectedMonth);
              int maxDays=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
              dayList.removeAllItems();
              for(int i=1;i<=maxDays;i++)
                   dayList.addItem(String.valueOf(i));
         public void fillCurrentDate()
                   Calendar cal=Calendar.getInstance();
                   showDays(cal.get (Calendar.DAY_OF_MONTH));
    HAPPY CODING...
    regards
    Bandi
    mail : [email protected], [email protected]
    ****************** Think Green, Save Green ***************************

  • Getting incorrect week while using Calendar class

    when i ran the following file , I am getting the incorrect results.
    import java.util.*;
    public class Week
    public static void main( String args[] ) throws Exception
    //String sdate = "30-DEC-2006";
    String sdate = "31-DEC-2006";
    String sdate1 = "01-JAN-2007";
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MMM-yyyy");
    java.util.Date dt = sdf.parse(sdate);
    java.util.Date dt1 = sdf.parse(sdate1);
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.SUNDAY);
    calendar.setMinimalDaysInFirstWeek(7);
    calendar.setTime(dt);
    System.out.println("Week of year for 31-DEC-2006 is : =>"+calendar.get(Calendar.WEEK_OF_YEAR));
    calendar.setTime(dt1);
    System.out.println("Week of year for 01-JAN-2007 is : =>"+calendar.get(Calendar.WEEK_OF_YEAR));
    output:
    Week of year for 31-DEC-2006 is : =>53
    Week of year for 01-JAN-2007 is : =>53
    Can anyone tell me how to fix this issues.
    Thanks

    Did you listen about documentation? It takes about one minute to find followed:
    For example, a specific Calendar subclass may designate the week before week 1 of a year as week n of the previous year.
    You should spend few minutes more to find how to fix it.

  • Gregorian Calendar class

    Hi, i am new to this class and would like to know if anyone could help me. I am trying to do a bookings form and have 3 comboboxes which will hold the days, months, and a specified no. of years. I would like to know how i can use this G.C class to do so, and if there are any tutorials.
    Also for these combos do i need to specify the no. of days manually or can i obtain this from the G.C class. i have looked at the api but aint sure where to start.
    Any help appreciated
    thanks

    RTFM
    GregorianCalendar cal = new GregorianCalendar();
    int year = cal.get(Calendar.YEAR)

  • Imports, DataRow class, Calendar class

    Hello All,
    I am doing a
    DataRow lookupRow = new DataRow(queryDataSet2, "EMP_NO");
    //this is code from one of the sample files
    and jdev 1.1 keeps giving me an error
    "error 257: class DataRow not found in path..."
    but I have all the necessary imports,
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.lang.*; // for String functions
    import java.lang.Object.*; // for toString, calendar, etc
    import borland.jbcl.layout.*;
    import borland.jbcl.control.*;
    import borland.sql.dataset.*;
    import borland.jbcl.model.*;
    Same thing for this code:
    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy");
    Calendar rightNow = Calendar.getInstance();
    JDev gives error 257 for Calendar and SimpleDateFormat also. Any
    help appreciated.
    Thanks in advance,
    null

    Hello All,
    Finally got the imports figured out. Of course, JDev does not
    cascade includes in any way, ie it is not similiar to header
    files in C, but still I have noticed that ver 1.1 is quite buggy
    in terms of recognizing when an object has been declared.
    HTH someone,
    null

  • What is wrong with Calendar class?

    I have a J2EE app, I'm using WebServices, EJB3 and JSF... my jsf pages call to EJBs and the EJBs call to webServices to obtain data from DB, the returned data is a custom object array (ex. Class Cost[]), each Cost Object has a Calendar member (that is required because Calendar has its representation as xs:dateTime in SOAP)... i call to webservices methods, and i obtain the data, at this time all is fine, but when the object is loaded in the EJB remote, the Cost elements lose some part of themselves, lose some part of your own Calendar... in this point i can't obtain the MONTH or the TimeZone of the Calendar object inside the Cost object, i excecute costs[0].getMoment().get(Calendar.MONTH) and results in NullPointerException... I has been inspecting the calendar object within debugging and i found that "gdate" variable is null and this generates the Exception.
    I hope that someone understand this post. Sorry about my english.

    I found that the problem is present in the serialization when the remote object is instantiated.
    Now, I had to change the call to the local object instance, and the problem disappear.
    I think, that is a bug of Java or the JBoss Implementation, currently I'm using jboss-4.0.4.CR2.

  • Calendar Class with DateFormat

    Hello,
    I thought I posted this message before, but I can't find it. I'm dead tired so please let me know if I make this too short to follow. I am making a GUI program using BreezySwing (the only GUI class I know), and I am going to have it use the time to tell me how far away certain events that I input are. After digging in the documentation for a few hours :( and playing around with it to get a feel, and I ended up coming up with a line of code that gives the time with Calendar.VARIABLE variables (i.e. HOUR, MINUTE, SECOND, etc). I am putting those variables into a message in a text area. The problem, which persists even when I put a Calendar.getInstance() in, is that it always returns/displays the same time. I wondered about it caching something, so I rebooted and even shut down overnight and still SAME TIME.
    Any ideas? I am just doing this for fun from my year of Java in high school a couple years ago for now but I am leaning toward majoring in CS now in college so I will want to get a thorough knowledge (if that matters in your response).
    Thanks in advance,
    Shaun

    You'll probably get a better response if you post the
    code you've tried so far...I know you want to help. Let me just advise you that bringing up days-old threads are somewhat an exercise in futility, and also be aware that by looking at the posting history you can see that the OP duplicated this and received responses on the duplicate.

Maybe you are looking for