GregorianCalendar

hi
i want to get the number of days from previous month, how can i go about doing it?
if i use today.get(Calendar.DAY_OF_MONTH), i will get the current number of days
please help
thanks!!

Try
today.add(Calendar.MONTH, -1).getActualMaximum(Calendar.DAY_OF_MONTH);

Similar Messages

  • Want to make Monday as the first day of the week in GregorianCalendar. how?

    hi
    I need to know what day is the first of the month is. for example the 1st of Nov 2004 is Moday and 1st of Dec 2004 is saturday.
    I am using the GregorianCalendar:
    1.  GregorianCalendar calendar = new GregorianCalendar(2004,11,1);    //set date to 1st Nov 2004
    2.  int firstDay = calendar.get(Calendar.DAY_OF_WEEK);now firstDay is 2. This is because the week starts from Sunday, so Monday is the 2nd day.
    But I am in Uk and my Uk Calendar shows monday as the first day (even in Windows 2000 Calendar).
    I would like the GregorianCalendar to have Monday as the first day of the week, so that the
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    returns 1 in the above case.
    I also tried adding
    calendar.setFirstDayOfWeek(Calendar.MONDAY); just between line 1 and line 2, but it did not help.
    This is because i don't want to manually subtract 1. If i manually subtract one, then the program might not work in other locale and timezones.
    Also if I subtract 1, then for 1 Feb 2004,
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    will return 1 as 1st Feb 2004 falls on Sunday. so if i subtract 1 it will be 0, so I have to do a extra checking for 0.
    Is there anyway to make the Calendar have Monday as the first day of the week??
    Tanveer

    hi
    I need to know what day is the first of the month is.Why does this matter? Since we know that 1 == Sunday and 2 == Monday... Why do you need Monday to be == 1? and couldn't you just subtract 1 if it's so important?
    for example the 1st of Nov 2004 is Moday and 1st of
    Dec 2004 is saturday.
    I am using the GregorianCalendar:
    1.  GregorianCalendar calendar = new
    GregorianCalendar(2004,11,1);    //set date to 1st Nov
    2004
    2.  int firstDay = calendar.get(Calendar.DAY_OF_WEEK);now firstDay is 2. This is because the week starts
    from Sunday, so Monday is the 2nd day.
    But I am in Uk and my Uk Calendar shows monday as the
    first day (even in Windows 2000 Calendar).
    I would like the GregorianCalendar to have Monday as
    the first day of the week, so that the
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    returns 1 in the above case.
    I also tried adding
    calendar.setFirstDayOfWeek(Calendar.MONDAY);[/cod
    ] just between line 1 and line 2, but it did not help.
    This is because i don't want to manually subtract 1.
    If i manually subtract one, then the program might not
    work in other locale and timezones.
    Also if I subtract 1, then for 1 Feb 2004,
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    will return 1 as 1st Feb 2004 falls on Sunday. so if i
    subtract 1 it will be 0, so I have to do a extra
    checking for 0.
    Is there anyway to make the Calendar have Monday as
    the first day of the week??
    Tanveer

  • Moving a GregorianCalendar to the first day of the week

    I'm using a GregorianCalendar class in order to get the time stamp for the nearest Sunday's date.
    here's what I'm trying to do:
    1. get a timeStamp and set it into a date
    Date startDate = new Date(startTime);2. create a new GregorianCalendar using the above date as the start time.
    GregorianCalendar calendar = new GregorianCalendar((startDate.getYear()+1900),startDate.getMonth(),startDate.getDate());3. the tricky part: make sure the calnedar's date is set to Sunday, if the current date is on any day other then Sunday, I will have to move it back to the nearest Sunday. I'm trying to do this by using:
    calendar.set(Calendar.DAY_OF_WEEK, 0);     for some reason this does not work...
    am I doing anything wrong here?

    thanks for that info but the problem is still there. here's some more input:
    //creating the date, the ts here is 1153858800000 which is the dated in 7/25/2006
    Date startDate = new Date(startTime);
    //creating the new calendar
    GregorianCalendar calendar = new GregorianCalendar((startDate.getYear()+1900),startDate.getMonth(),startDate.getDate());
    //setting the calendar to the first hour (not sure it necessary)
    calendar.set(Calendar.HOUR, 1);
    //setting the calendar to the first day of the week
    calendar.set(Calendar.DAY_OF_WEEK, 1);
    //this returns 1153778400000 which is still dated 7/25/2006 whic is a Tuesday I would expect it to returns a different date
    calendar.getTimeInMillis()     

  • How to create a GregorianCalendar with timezone information

    Hello All,
    Can any one tell me how to create a GregorianCalendar with time zone information i.e
    I have following information with me
    year
    month
    day
    hour
    minutes
    seconds
    deci-seconds
    direction from UTC "+" / "-" (in ascii notation)
    hours from UTC minutes from UTC
    How can i construct a calender out of it?

    How to construct a calendar:
    Calendar.getInstance()
    Did you consider looking at the API docs with the set() method for calendar and the field constants for the values you can set?
    Although I wonder whether it's simpler to just concatenate the data to a String and parse it to a Date instance using SimpleDateFormat.

  • Error with gregoriancalendar

    I'm having an error with the gregoriancalendar class.
    I have a class that gets an initial date froma database, and iterate until today.
    The problem is that its showing that today is day 262 and we are in day 232 of the year.
    Whats the problem????
    Any help will be appreciate.
    the code is bellow:
    String sql2 = "Select d02inter from cen02";
              try{
              PreparedStatement st4 = cnn3.prepareStatement(sql2);
              ResultSet rs2 = st4.executeQuery();
              dataatual = null;
              while (rs2.next()){
                   String dataint = rs2.getString("d02inter");
                   Integer ano_int = Integer.valueOf(dataint.substring(1,5));
                   Integer mes_int = Integer.valueOf(dataint.substring(5,7));
                   Integer dia_int = Integer.valueOf(dataint.substring(7));
                   Calendar calend_int = new GregorianCalendar(ano_int,mes_int,dia_int);
                   Calendar calend_hoje = new GregorianCalendar();
                   Calendar calendwork = (GregorianCalendar)calend_int.clone();
                   while((calendwork.compareTo(calend_hoje))<0){
                        Integer diadoano=calendwork.get(Calendar.DAY_OF_YEAR);
                        Integer ano = calendwork.get(Calendar.YEAR);
                        dataatual=(ano*1000)+diadoano;
                        if(calendwork.equals(calend_int)){
                             if(mp.containsKey(dataatual)){
                                  bmhBean bm = (bmhBean)mp.get(dataatual);
                                  bm.entrou++;
                                  mp.put(dataatual,bm);
                             }else{
                                  bmhBean bm = new bmhBean();
                                  bm.entrou++;
                                  mp.put(dataatual,bm);
                        if(mp.containsKey(dataatual)){
                             bmhBean bm = (bmhBean)mp.get(dataatual);
                             bm.inter++;
                             mp.put(dataatual,bm);
                        }else{
                             bmhBean bm = new bmhBean();
                             bm.inter++;
                             mp.put(dataatual,bm);
                        calendwork.add(Calendar.DATE,1);
              }

    Yes I agree with Wildcard82
    The GregorianCalendar javadoc defines the month 0 based (0 - 11)
    so you must be careful when creating a Gregorian calendar by substracting 1 to your month number (if 1 - 12), and if passing it back to an int of the type 1 - 12, to add a respective 1e on the
    get(GregorianCalendar.MONTH)method
    I recommend what i did to evade dealing with this, I'm used to save dates in the yyyymmdd format, and month being from 1-12, so I created two methods
    a) int2GregorianCalendar(int date), which takes that int type and returns the correct GregorianCalendar, with what was defined above.
    b) GregorianCalendar2int(GregorianCalendar date) which is the opposite.
    Hope it helps, cya.

  • Parsing datestring to GregorianCalendar on Solaris.

    Hi There!
    I?m parsing strings to GregorianCalendar object with the class SimpleDateFormat. The problem is that when I?m running on a Windows platform everything works out great but when I deploy it on a Solaris platform the parser parse it wrongly.
    Heres the code:
    java.util.TimeZone tz= null;
    String timezonesettings= null;
    String availableIds[]= null;
    String timezone= null;
    SimpleTimeZone zone= null;
    java.text.SimpleDateFormat sdf= null;
    int TZ_OFFSET= 1*60*60*1000;
    try{
    //get requested time zone from jsp page.
    timezone= request.getParameter("timezone");
    //pattern= "ww-yyyy"
    pattern= request.getParameter("pattern");
    //get requested datestring from client
    date= request.getParameter("date");
    loc= Integer.parseInt(request.getParameter("locale"));          
    zone = new SimpleTimeZone(TZ_OFFSET, timezone);
    zone.setStartRule(Calendar.MARCH,-1,Calendar.SUNDAY,2*60*60*1000);
    zone.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
    sdf= new java.text.SimpleDateFormat(pattern,locale[loc]);
    sdf.setTimeZone(zone);
    sdf.setLenient(false);
    gregdate= new GregorianCalendar(zone,locale[loc]);
    gregdate.setTime(sdf.parse(date));
    }catch(Exception e){
    The thing is when i try to parse with the pattern "ww-yyyy" the parsing on a solaris platform always returns a the date with value 1 Jan. Why this??
    Best regards
    Niclas Rothman

    It is a known bug:
    http://developer.java.sun.com/developer/bugParade/bugs/4227284.html
    See their work around.

  • Problems with GregorianCalendar.add(Calendar.WEEK_OF_YEAR, 1)

    hi all
    i'm writing an application to calculate all the days of all the week of the year
    i got 2 buttons, one to forward to the next week, the other to go back to the last
    this is my code:
    GregorianCalendar now = new GregorianCalendar();
    DateFormat week=new SimpleDateFormat( "w" ); 
    private void b_proxActionPerformed(java.awt.event.ActionEvent evt)
    //the next week
            now.add(now.WEEK_OF_YEAR, 1);                      
            System.out.println("week "+week.format(now.getTime()));
    private void b_precActionPerformed(java.awt.event.ActionEvent evt) {
            now.add(now.WEEK_OF_YEAR, -1);    
             System.out.println("week "+week.format(now.getTime()));
    }the problem is that, when i want to skip to the next week i get:
    WEEK 25 (the current week)
    WEEK 26
    WEEK 28
    WEEK 30
    like i were add 2, but i added 1 to the week value!!!
    and when i want to return to the previous week (by pressing the second button), the week number is always the same!!
    anyone could help me?
    thanx
    sandro

    Whether it matters or not, just simply stating that it's correct.
    Here's my code that worked correctly ...
    Calendar time = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("w");
    for(int i=0; i < 10; i++)
        System.out.println("week in year: "+time.get(Calendar.WEEK_OF_YEAR));
        System.out.println("Week in year by sdf: "+sdf.format(time.getTime()));
        time.add(Calendar.WEEK_OF_YEAR, 1);
    }

  • FileNotFoundException at new GregorianCalendar() in 1.4.2_02

    The following exception is thrown in windows platform of j2sdk 1.4.2_02 (also in 03) when I create a new GregorianCalendar
    Thread [main] (Suspended (exception FileNotFoundException))
         FileInputStream.open(String) line: not available [native method] [local variables unavailable]
         FileInputStream.<init>(File) line: 106
         ZoneInfoFile$1.run() line: 910
         AccessController.doPrivileged(PrivilegedExceptionAction) line: not available [native method]
         ZoneInfoFile.readZoneInfoFile(String) line: 904
         ZoneInfoFile.createZoneInfo(String) line: 520
         ZoneInfoFile.getZoneInfo(String) line: 499
         TimeZone.parseCustomTimeZone(String) line: 633
         TimeZone.getTimeZone(String, boolean) line: 450
         TimeZone.getDefault() line: 522
         GregorianCalendar.<init>() line: 336
         MyTest.<clinit>() line: 28The source is
    public class MyTest {
         private static Calendar cal = new GregorianCalendar();
         Does anyone know of any cure?
    Thanks a lot.
    Gary.

    I don't think you can do new GregorianCalendar, but you need to use Calendar.getInstance().
    Read more at:
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html
    Here is some:
    Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on. (A Date object represents a specific instant in time with millisecond precision. See Date for information about the Date class.)
    Subclasses of Calendar interpret a Date according to the rules of a specific calendar system. The platform provides one concrete subclass of Calendar: GregorianCalendar. Future subclasses could represent the various types of lunar calendars in use in many parts of the world.
    Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar's getInstance method returns a Calendar object whose time fields have been initialized with the current date and time:
    Calendar rightNow = Calendar.getInstance();
    A Calendar object can produce all the time field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
    Gil

  • GregorianCalendar method add(int,int) in websphere5.1

    This bug report landed in my lap. The program is registering payments, and when user is registering multiple payments he gets the duedate set to 1970 from the second payment and on. Someone wrote on the bug report "method add(int,int) in GregorianCalendar doesn't work in WebSphere 5.1".
    So, when t>0 in the following code, things go haywire. Anyone have experience with this?
    dueDateG.clear();
    dueDateG.setLenient(false);
    dueDateG.setTime(dueDate);
    if (t==0)   addMonth = 0;
    else        addMonth = freq;
    dueDateG.add(Calendar.MONTH, addMonth);
    date = dueDateG.getTime();
    dueDate = date;

    By "Websphere 5.1" are you meaning Websphere Application Server 5.1 or Websphere Application Developer 5.1?
    The former (as known as WAS) uses IBM JDK 1.4.X (I believe that's 1.4.1); the later (as known as WSAD) can be used with any version of WAS starting from 4.X I believe.
    Some things can have problems when you're using IBM JDK's instead of Sun's, for instance trying to use the javax.crypto.* classes, that are different from Sun's; but I don't know if GregorianCalendar has problems. The first thing to do is insulate the code that you sent and try running it in IBM's and Sun's JDK. If you get problems only with IBM's JDK, you'll need to rewrite your code.

  • GregorianCalendar datetime format convert to  sql server date time format

    please help me
    my GregorianCalendar date time format is like this. *08/01/29 02:25:59* . I try to insert my database(sql server 2000 )
    data type is datetime ,in my database table display like this *2029-08-01 02:25:59.000* .can you help me to insert correct date in my database table like ( . *08/01/29 02:25:59*)

    use [PreparedStatement |http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html] and setTimestamp:
    [http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html]

  • Is this a bug with GregorianCalendar.getTimeMiliseconds()?

    Hi,
    I have used GregorianCalendar.getTimeMiliseconds() method for many years, but now I have a problem.
    The code below returns the same value:
    System.out.println("DATE1=" + new GregorianCalendar(2009,1,29,8,0,1).getTimeInMillis());
    System.out.println("DATE2=" + new GregorianCalendar(2009,2,1,8,0,1).getTimeInMillis());The reply for both is: 1235908801000
    Then, I think this is a Java bug, right?
    My system is:
    Linux Fedora Core + JDK 1.6.5.
    The same bug occurs in another machine:
    Windows 2003 Server + JDK 1.6.10.

    Edilmar_Alves wrote:
    Then, I think this is a Java bug, right?Nope. Your understanding of the [GregorianCalendar constructor|http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html#GregorianCalendar(int,%20int,%20int,%20int,%20int,%20int)] is incorrect. ;o)
    Months in Java are zero-indexed. You should never use int literals for the core calendar API. Use constants instead, and you'll get results more in line with yor expectations. Example:
    Calendar c1 = new GregorianCalendar(2009,Calendar.JANUARY,29,8,0,1);
    Calendar c2 = new GregorianCalendar(2009,Calendar.FEBRUARY,1,8,0,1);
    System.out.println("DATE1=" + c1.getTimeInMillis());
    System.out.println("DATE2=" + c2.getTimeInMillis());~

  • Strange behavior of GregorianCalendar

    Hi all,
    There�s a strange behavior of GregorianCalendar class which spend my time and my patience. After I discovered the problem, I run this code...
    System.out.println("Data1: " + gcTemp.get(gcTemp.DAY_OF_MONTH));
    gcTemp.add(gcTemp.DAY_OF_MONTH, 1);
    System.out.println("Data2: " + gcTemp.get(gcTemp.DAY_OF_MONTH));
    ...inside a looping. Them, I got this output:
    Data1: 10
    Data2: 11
    Data1: 11
    Data2: 11
    Data1: 12
    Data2: 13
    Notice two "11"s. What hell is it? The gcTemp is instanced one time, there�s no concurrent thread change its value.
    Well, today is 2003-10-13 and it�s running in a Brazilian timezone.
    This strange behavior make me test if the day was changed, then, if not, redo the increment. (...).
    Thanks,
    Leonardo Rodrigues

    Is that Brazilian timezone one of those where daylight saving time is observed? And did daylight saving time start on the 12th of the month that you didn't print out? In fact, why not print out the entire date instead of just the month? That might lead you to the answer.

  • GregorianCalendar Issue

    Hi,
    In the process of finding the difference between two dates, I am encountering a problem where one of the dates when converted to GregorianCalendar always prints a -ve value. Would you have any hints as to why would that would be?
         String fmt = "MM-dd-yyyy HH:mm:ss";
         SimpleDateFormat df = new SimpleDateFormat(fmt);
         Date expires = null;
    try
    expires = df.parse(endTime);
    catch (ParseException pe)
    pe.printStackTrace();
    GregorianCalendar gexpires = new GregorianCalendar();
    gexpires.setTime(expires);
    GregorianCalendar rightNow = new GregorianCalendar();
    long exp_millies = gexpires.getTimeInMillis();
    long now_millies = rightNow.getTimeInMillis();
    exp_millis is always -ve, even though I am parsing the future date from an xml string.
    Just wondering where I am going wrong!! Thanks
    M

    What's the string you're passing for the date?
    Zero is Jan. 1, 1970, 00:00:00.000 GMT. Any dates prior to that are negative.

  • GregorianCalendar: I seem to always be five (5) hours head in my value.

    The following just outputs the milliseconds of the current datetime in Central Standard Time (CST).
    What I am try to test is this line:
    calendar.add(Calendar.HOUR_OF_DAY, 0);When I try the following:
    package test;
    import java.util.Date;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.SimpleTimeZone;
    import java.util.TimeZone;
    public class GetCal {
         public static void main(String args[]){
              String[] ids = TimeZone.getAvailableIDs(-6 * 60 * 60 * 1000);
              if (ids.length == 0) {
                  System.exit(0);
              System.out.println("Current Time");
              SimpleTimeZone pdt = new SimpleTimeZone(-6 * 60 * 60 * 1000, ids[0]);
              // set up rules for daylight savings time
              pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
              pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
              Calendar calendar = new GregorianCalendar(pdt);
              Date trialTime = new Date();
              calendar.setTime(trialTime);
              calendar.add(Calendar.HOUR_OF_DAY, 0);
              long nCalTime = calendar.getTimeInMillis();
              String nCalTimeValue = String.valueOf(nCalTime);
              System.out.println("Long " + nCalTime);
              System.out.println("String " + nCalTimeValue);
    }I would output something similar to:
    1149624386040
    I would take this value and run it in the following SQL to verify the datetime:
    VARIABLE millis NUMBER
    EXEC :millis := 1149624386040;
    ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH:MI:SS AM';
    SELECT TO_DATE('197001','YYYYMM') + (:millis / 1000 / 60 / 60 / 24) converted
    FROM DUAL;The result would be:
    06-JUN-2006 08:06:26 PM
    However, the actual time would be:
    06-JUN-2006 03:06:26 PM
    Thus, I am five hours ahead.
    Since I am adding zero (0) in:
    calendar.add(Calendar.HOUR_OF_DAY, 0);I thought I should get the correct time, however I always seem to be five hours ahead.
    I'd greatly appreciate any help or corrects.
    Thank you,
    --Todd                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    DrClap: I greatly appreciate your help with this.
    I ran the following:
    select DBTIMEZONE from dual;This returned "-06:00".
    In a nutshell, I am testing with the current date - but what I have is a JSF MyFaces Calendar component that represents a date and a JSF listbox with the values 0 - 23 as hours options.
    A user will pick a date and select an hour.
    I have converted the date to milliseconds, add the selected hour and the resulting value needs to be in milliseconds.
    This value will be passed to a method that will return a list of transactions for the user's picked date and selected hour. I have no control of the method - I am only consuming.
    I am testing with the current date, to make sure everything is converting to milliseconds correctly - which it's not since I seem to be five (5) hours ahead.
    Now that I know that the Oracle database server as a DBTIMEZONE value of "-06:00"; do I just create a static variable of (5 hours) 18000000 and just subtract that from the millisecond value that is made up of the picked date and selected hour?
    There has to be a better way?
    Thanks,
    --Todd                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How to know the which week in GregorianCalendar API

    Dear all
    does someone know that way when i use GregorianCalendar this API to know the which week is
    for example in this month has 6 week and 2008/08/27 is week 5 , dose any idea i can know it in this program
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.Date;
    import java.text.DateFormat;
    public class DateExample5 {
         public static void main(String[] args) {
              DateFormat dateFormat =
              DateFormat.getDateInstance(DateFormat.FULL);
              // Create our Gregorian Calendar.
              //GregorianCalendar cal = new GregorianCalendar(2008,05,10);
              GregorianCalendar cal = new GregorianCalendar();
              //cal.setTime(new Date());
              System.out.println("System Date: " +dateFormat.format(cal.getTime()));
              // Set the day of week to FRIDAY
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.SUNDAY);
              System.out.println("After Setting Day of Week to Friday: " + dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.MONDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.TUESDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.WEDNESDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.THURSDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.FRIDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.SATURDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.add(GregorianCalendar.DAY_OF_MONTH,7);
    }Edited by: roger5089 on Aug 26, 2008 9:25 AM

    import java.util.Calendar;
    import static java.util.Calendar.WEEK_OF_MONTH;
    public class DateExample5 {
        public static void main(String[] args) {
            Calendar cal = Calendar.getInstance();
            int weekOfMonth = cal.get(WEEK_OF_MONTH);
            System.out.println(weekOfMonth); //5
    }Never used Calendar.WEEK_OF_MONTH, but I had a butcher's hook and found this documentation:
    <quote>
    Field number for get and set indicating the week number within the current month. The first week of the month, as defined by getFirstDayOfWeek() and getMinimalDaysInFirstWeek(), has value 1. Subclasses define the value of WEEK_OF_MONTH for days before the first week of the month.
    </quote>

  • Formatting a GregorianCalendar object for printing

    Hello,
    I've got a JLabel in which I wish to put a GregorianCalendar object as a string. Now if I just print it out there it doesn't look as I wish... I basically just want the year, month and day to be there (and if possible in a format of my choice), not the time or the timezone.
    How do I do this? Probably it's easy peasy but I've tried looking through the API and it didn't help me much in this matter so help is appreciated.
    Thanks in advance.

    Use SimpleDateFormat.

Maybe you are looking for