Java date prcocuring doubt

Hi Experts...
I want to produce the previous week's monday as start date and previous week's sunday as end date in two String variables.
The format is ddMMMyyyy.
These dates have to be generated irrespective of the day of the current week.
How do i achieve this.
Please help...
gaugetaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Get a Calendar and iterate backwards [cal.add(Calendar.DATE, -1);]until you get to a Calendar.MONDAY. Format that date. Add the correct number of days to get you to Sunday. Format that date
0
gaugetaAuthor Commented:
@CEHJ:Thanks for the reply.
Say for example if today i thursday and i get a Calendar and iterate backwards until i get Monday then it will be this week's monday instead of the previous week's monday.
How do i avoid this.
Please help...
0
for_yanCommented:

try this - works for me

   java.util.Date ddnow = new java.util.Date();

        long ago = ddnow.getTime() - 24L*1000L*3600L*14L;

        java.util.Date dateSun = null;
         java.util.Date dateMon = null;

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");


        for(int j=0; j<14; j++){
            java.util.Date curd = new java.util.Date(ago + 24L*3600L*1000L*j);
                       if(curd.getDay() == 0 && dateSun == null)dateSun = new java.util.Date(curd.getTime() + 24L*3600L*1000L*7L);
                       if(curd.getDay() == 1 && dateMon == null)dateMon = curd;


        }

        System.out.println("Mon " + sdf.format(dateMon));
          System.out.println("Sun " + sdf.format(dateSun));

Open in new window


Mon 03-Oct-2011
Sun 09-Oct-2011

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

gaugetaAuthor Commented:
@for_yan:Thanks for the reply.
Will try and get back if i face any issues.
Will the code posted below work for the same.

GregorianCalendar Cal=new GregorianCalendar();
Cal.set(GregorianCalendar.DAY_OF_WEEK,Calendar.Monday); 
Cal.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1); 
Cal.set(GregorianCalendar.DAY_OF_WEEK,Calendar.); 
Cal.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1);

Open in new window

0
for_yanCommented:
The above code may write warnings that getDay() is deprecated - don't pay attention to it - it works fine - it can be done with
Calendar, but with getDay() it is much more natural
0
gaugetaAuthor Commented:
Corrected code
GregorianCalendar Cal=new GregorianCalendar();
Cal.set(GregorianCalendar.DAY_OF_WEEK,Calendar.Monday); 
Cal.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1); 
Cal.set(GregorianCalendar.DAY_OF_WEEK,Calendar.SUNDAY); 
Cal.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1);

Open in new window

0
for_yanCommented:
Perhaps your code  should work - test it;
my code seems more natural to me, even though I also think it should be tested with different starting days.
I'll do it
0
gaugetaAuthor Commented:
@for_yan:Thanks a lot.
I'll update as soon as i try this.
Not at my system now.
0
for_yanCommented:
So if today is sunday - what monday do you want ?
0
for_yanCommented:
so suppose I would be doing it on Oct 9th - do you want to have mon oct 3th or mon sep 26th - ?
0
for_yanCommented:
This would work, if you don't want to end on today's sunday:

 java.util.Date ddnow = java.sql.Date.valueOf("2011-10-10");




        long numbeg = 14L;
        if(ddnow.getDay() ==  0) numbeg = 15L;
        if(ddnow.getDay() == 1)numbeg = 10L;

        long ago = ddnow.getTime() - 24L*1000L*3600L*numbeg;

        java.util.Date dateSun = null;
         java.util.Date dateMon = null;

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");


        for(int j=0; j<14; j++){
            java.util.Date curd = new java.util.Date(ago + 24L*3600L*1000L*j);
                       if(curd.getDay() == 0 && dateSun == null)dateSun = new java.util.Date(curd.getTime() + 24L*3600L*1000L*7L);
                       if(curd.getDay() == 1 && dateMon == null)dateMon = curd;


        }

        System.out.println("Mon " + sdf.format(dateMon));
          System.out.println("Sun " + sdf.format(dateSun));

Open in new window

0
for_yanCommented:
if you are OK with today's sunday as the end day then this works:

        java.util.Date ddnow = new java.util.Date();


        // java.util.Date ddnow = java.sql.Date.valueOf("2011-10-10");




        long numbeg = 14L;
        if(ddnow.getDay() ==  0) numbeg = 10L;
        if(ddnow.getDay() == 1)numbeg = 10L;

        long ago = ddnow.getTime() - 24L*1000L*3600L*numbeg;

        java.util.Date dateSun = null;
         java.util.Date dateMon = null;

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");


        for(int j=0; j<14; j++){
            java.util.Date curd = new java.util.Date(ago + 24L*3600L*1000L*j);
                       if(curd.getDay() == 0 && dateSun == null)dateSun = new java.util.Date(curd.getTime() + 24L*3600L*1000L*7L);
                       if(curd.getDay() == 1 && dateMon == null)dateMon = curd;


        }

        System.out.println("Mon " + sdf.format(dateMon));
          System.out.println("Sun " + sdf.format(dateSun));

Open in new window

0
gaugetaAuthor Commented:
@for_yan:Thanks for the reply.
For the comment "so suppose I would be doing it on Oct 9th - do you want to have mon oct 3th or mon sep 26th - ? " i want Sep 26th  as the monday to be considered.
0
for_yanCommented:
Then the code  in posting ID:36947637
should work for you
0
for_yanCommented:
I think I tried this for all days of the week:

        java.util.Date ddnow = new java.util.Date();


         //java.util.Date ddnow = java.sql.Date.valueOf("2011-10-10");




        long numbeg = 14L;
        if(ddnow.getDay() ==  0) numbeg = 15L;
        if(ddnow.getDay() == 1)numbeg = 10L;

        long ago = ddnow.getTime() - 24L*1000L*3600L*numbeg;

        java.util.Date dateSun = null;
         java.util.Date dateMon = null;

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");


        for(int j=0; j<14; j++){
            java.util.Date curd = new java.util.Date(ago + 24L*3600L*1000L*j);

                    
                       if(curd.getDay() == 0 && dateSun == null)dateSun = new java.util.Date(curd.getTime() + 24L*3600L*1000L*7L);
                       if(curd.getDay() == 1 && dateMon == null)dateMon = curd;


        }

        System.out.println("Mon " + sdf.format(dateMon));
          System.out.println("Sun " + sdf.format(dateSun));

Open in new window

0
for_yanCommented:

This is the same thing without deprecation.
Amazing how much less convenient and less intuitive they made it with this Calendar.
Nevertheless this code works:

        java.util.Date ddnow = new java.util.Date();


         //java.util.Date ddnow = java.sql.Date.valueOf("2011-10-10");



        Calendar calend = Calendar.getInstance();

        calend.setTime(ddnow);




        long numbeg = 14L;
       // if(ddnow.getDay() ==  0) numbeg = 15L;
       // if(ddnow.getDay() == 1)numbeg = 10L;
        if(calend.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)numbeg = 15L;
         if(calend.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY)numbeg = 10L;



        long ago = ddnow.getTime() - 24L*1000L*3600L*numbeg;

        java.util.Date dateSun = null;
         java.util.Date dateMon = null;

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");


        for(int j=0; j<14; j++){
            java.util.Date curd = new java.util.Date(ago + 24L*3600L*1000L*j);

                     calend.setTime(curd);
                       if(calend.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY && dateSun == null)dateSun = new java.util.Date(curd.getTime() + 24L*3600L*1000L*7L);
                       if(calend.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY && dateMon == null)dateMon = curd;


        }

        System.out.println("Mon " + sdf.format(dateMon));
          System.out.println("Sun " + sdf.format(dateSun));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gaugetaAuthor Commented:
@for_yan:Thanks a lot.Works perfectly.
Can you describe the code in brief.
Thanks again.
0
for_yanCommented:
It goes 14 days back using this constructir which takes long milliseconds and then cans forward and finds first sunday (and adds a week) and first monday. As this number 14 may depend on the day of the week I tried with differnet starting date (changed today :)) and found that in some cases we need to take less or more - it is easier to do it experimentally than to count
0
for_yanCommented:
Actually it was smarter to scan backwards and to look for the first sunday but not today and the easily count from there.
Anyway, once it works, it is ok.
0
gaugetaAuthor Commented:
@for_yan:Thanks again.
Yes counting backwards was simpler.Thanks anyway.
0
for_yanCommented:
Yes, this is better:

public class FindDate {

    public static void main(String[] args) {


        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");

        Calendar calend1 = Calendar.getInstance();

            // calend1.add(Calendar.DATE, +10);
        
        java.util.Date today = calend1.getTime();

        int day = -1;
        while(true){
            calend1.add(Calendar.DATE, day);
            if(calend1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) break;

        }
        java.util.Date ddsun = calend1.getTime();
                    calend1.add(Calendar.DATE, -6);
        java.util.Date ddmon = calend1.getTime();


         System.out.println("Today: " + sdf.format(today));
        System.out.println("Monday: " + sdf.format(ddmon));
             System.out.println("Sunday: " + sdf.format(ddsun));
}
}

Open in new window


Output:
Today: 13-Oct-2011
Monday: 03-Oct-2011
Sunday: 09-Oct-2011

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.