GAUTAM
asked on
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...
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...
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
ASKER
@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...
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...
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));
Mon 03-Oct-2011
Sun 09-Oct-2011
ASKER
@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.
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);
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
Calendar, but with getDay() it is much more natural
ASKER
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);
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
my code seems more natural to me, even though I also think it should be tested with different starting days.
I'll do it
ASKER
@for_yan:Thanks a lot.
I'll update as soon as i try this.
Not at my system now.
I'll update as soon as i try this.
Not at my system now.
So if today is sunday - what monday do you want ?
so suppose I would be doing it on Oct 9th - do you want to have mon oct 3th or mon sep 26th - ?
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));
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));
ASKER
@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.
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.
Then the code in posting ID:36947637
should work for you
should work for you
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));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@for_yan:Thanks a lot.Works perfectly.
Can you describe the code in brief.
Thanks again.
Can you describe the code in brief.
Thanks again.
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
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.
Anyway, once it works, it is ok.
ASKER
@for_yan:Thanks again.
Yes counting backwards was simpler.Thanks anyway.
Yes counting backwards was simpler.Thanks anyway.
Yes, this is better:
Output:
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));
}
}
Output:
Today: 13-Oct-2011
Monday: 03-Oct-2011
Sunday: 09-Oct-2011