7

I would like to fetch the first date of a week.

My input is going to be a String type like 07/26/2014".

I need to get the first date of week in which the above date(07/26/2014) falls.

I need output date in MM/dd/YYYY format .

basically I need output as 07/21/2014.

Please give me the java program. I have done upto this

SimpleDateFormat formatter1 = new SimpleDateFormat("MM/dd/yy");
String date ="07/26/2014";
    Date Currentdate = formatter1.parse(date);
    int currentday=Currentdate.getDay();


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Currentdate);
        int startDay=currentday-calendar.getFirstDayOfWeek();
        Currentdate.setDate(contacteddate.getDate()-startDay);
        System.out.println(contacteddate.getDate());
    }

The above code only gives me the date.. I need date along with month and year in "MM/dd/YYYY" Please help

2

4 Answers 4

5

I would do it this way

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(Currentdate);
    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
1
  • 4
    Note that this depends on what the first day according to the given locale is. For someone in the US the answer for 07/26/2014 will be the 20th and not the 21st. Just something to take into account.
    – Voo
    Jul 5, 2014 at 8:59
4

After setting time to Calendar

Calendar calendar = Calendar.getInstance();
calendar.setTime(Currentdate);

use

calendar.set(Calendar.DAY_OF_WEEK, 1)

and then

simpleFormat.format(calendar.getTime());
3
  • I think OP wants MONDAY, not SUNDAY = 1. Your code yields one day earlier in US. Jul 7, 2014 at 10:11
  • @Meno first day of week depends on Locale you set Jul 7, 2014 at 15:58
  • Yes I know, the first day of week is dependent on the Locale. But maybe I was not quite clear. I referred to the line calendar.set(Calendar.DAY_OF_WEEK, 1). The number 1 stands for the constant Calendar.SUNDAY. And the OP wants "07/21/2014" which is a Monday. Jul 7, 2014 at 16:19
2

This will help you.

 // Get calendar set to current date and time
 Calendar c = Calendar.getInstance();

 // Set the calendar to monday of the current week
 c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

 // Print dates of the current week starting on Monday
 DateFormat df = new SimpleDateFormat("EEE dd/MM/yyyy");
 for (int i = 0; i < 1; i++) {
   System.out.println(df.format(c.getTime()));
   c.add(Calendar.DATE, 1);
 }
2

The problem with all presented solutions so far is not to specify what exactly the week definition is. Week definitions are either technically specified like in ISO-8601-standard (Monday as first day of week and first calendar week of year containing at least four days), or they use localized rules (for example in US a week begins by Sunday!).

Due to the requirement that the OP wants "07/21/2014" as first day of week around "07/26/2014" it seems that ISO-8601 is what the OP really wants. But code like

Calendar calendar = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
...

will not work in a country like US or an application server located in US. Counter example:

// simulating a US-located application server where this code is running
GregorianCalendar calendar = new GregorianCalendar(Locale.US);

calendar.set(2014, Calendar.JULY, 26);
calendar.getTime(); // avoid ugly side effects in calendar date handling
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(calendar.getTime())); // output: 2014-07-20

If the OP changes the choosen locale to let's say Locale.FRANCE (applying ISO-rules) then the OP can achieve his goal using the traditional Java-date-and-time-library.

It should be noted however that week handling using the java.util.Calendar-stuff is often confusing and hard. For example: Without the strange getter-call (calendar.getTime()) which enforces update of internal calculation the result would be: 2014-07-06 (surely not what OP wants).

Therefore I recommend following other libraries to choose a generic approach compatible with different week definitions:

a) Java-8 (built-in library JSR-310 aka java.time):

LocalDate date = LocalDate.of(2014, 7, 26);
TemporalField dowField = WeekFields.ISO.dayOfWeek();
date = date.with(dowField, dowField.range().getMinimum());
System.out.println(date); // output: 2014-07-21

Note: Avoid code like date.with(DayOfWeek.MONDAY) because in that case the java.time-library cannot evaluate the underlying week rules which possibly deviate from ISO-8601 (here choosen: WeekFields.ISO, but it might also be WeekFields.SUNDAY_START).

b) my own library Time4J:

PlainDate date = PlainDate.of(2014, 7, 26);
date = date.with(Weekmodel.ISO.localDayOfWeek().minimized());
System.out.println(date); // output: 2014-07-21

c) If you know in advance that you only want ISO-8601-week-rules then you might also consider a simpler approach in Java-8 or instead its predecessor JodaTime:

// Java-8 applying ISO-8601-rules
LocalDate date = LocalDate.of(2014, 7, 26);
date = date.with(DayOfWeek.MONDAY);

// Joda-Time
LocalDate date = new LocalDate(2014, 7, 26);
date = date.dayOfWeek().withMinimumValue();

Not the answer you're looking for? Browse other questions tagged or ask your own question.