Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching the first date of a week in java [duplicate]

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

like image 930
Surender Raja Avatar asked Jul 05 '14 06:07

Surender Raja


4 Answers

I would do it this way

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(Currentdate);
    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
like image 191
Evgeniy Dorofeev Avatar answered Nov 03 '22 10:11

Evgeniy Dorofeev


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());
like image 37
jmj Avatar answered Nov 03 '22 12:11

jmj


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);
 }
like image 2
Rahul Sharma Avatar answered Nov 03 '22 10:11

Rahul Sharma


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();
like image 2
Meno Hochschild Avatar answered Nov 03 '22 12:11

Meno Hochschild