Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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...
0
gaugeta
Asked:
gaugeta
  • 13
  • 7
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 13
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now