How to get week of selected date in java? - java

How to get week of particular selected date?
For Example:
My week will start from Monday and ends on Sunday.
So lets say i have selected 25 July 2017. So i want what was the date on monday of that week and what is the date on upcoming Sunday of that week.
The answer should be :: Monday -- 24 July 2017 AND Sunday-- 30 July 2017.
I am not able to find a simple way to get it.

You can see this. It is for the present date.
Calendar cal = Calendar.getInstance();
int week = cal.get(Calendar.WEEK_OF_MONTH);
int day = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(day);
Date mondayDate = null;
if (day > 2) {
int monday = day - 2;
cal.add(Calendar.DATE, -monday);
mondayDate = cal.getTime();
} else {
// cal.add(Calendar.DATE,);
mondayDate = cal.getTime();
}
int sunday = 7 - day + 1;
cal.add(Calendar.DATE, +sunday);
Date sundaydate = cal.getTime();
System.out.println(mondayDate);
System.out.println(sundaydate);
}
In this, we are finding the day of the week.Today we will get
day=2.
Now for monday,we will first check days.
if day=1, means it is sunday.
if day=2, means it is monday.
so for day>2, we are getting date of (day-2) days back. For today, day=1. hence mondaydate= 23 July,2017.
Similarily for sunday, we are getting date of (7-day+1) days later. For today, sunday=5, so after +6, sundaydate= 31 july,2017
Hope this helps :)

You can get like this :
String date = (String) android.text.format.DateFormat.format("dd", date);
String dayOfTheWeek = (String) DateFormat.format("EEEE", date);
For next Sunday you can calculate as per dayOfTheWeek.

Related

Getting the week number for a date (week starting on Wednesday)

LocalDate initial = LocalDate.now();
DayOfWeek dayOfWeek = DayOfWeek.WEDNESDAY;
WeekFields weekFields = WeekFields.of(dayOfWeek, 1);
int weekNo = date.get(weekFields.weekOfWeekBasedYear());
System.out.println("Week No"+weekNo);
I am using the above code for date 2018-07-29. I expect week no 30, but I get 31.
What am I missing here to get the result of 30?
If you expected output as according to ISO-8601, where current week is week 30, you'd need to follow this:
Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week that contains that year's first Thursday (='First 4-day week').
This is implemented by WeekFields.ISO.
If instead, you want the week to start on WEDNESDAY, you only need to change the minimalDaysInFirstWeek from 1 to 4 (='First 4-day week'):
LocalDate date = LocalDate.now();
WeekFields weekFields = WeekFields.of(DayOfWeek.WEDNESDAY, 4);
int weekNo = date.get(weekFields.weekOfWeekBasedYear());
System.out.println("Week No " + weekNo);

How to get the date of a specific day in a specific week

I wanto to get the date of a specific day in a specific week, identified by the week number.
Here's an example:
Today it is Monday (03.10.2016) and it is the week with the number 58.
Now i want to get the date of e.g. Friday of this week.
I use Joda-Time within my android application.
Currently Iam creating a LocalDate.
// This is the date of today
private LocalDate reportDate = new LocalDate();
// The number of the week is calculated here
int week = Weeks.weeksBetween(startDate.dayOfWeek().withMinimumValue().minusDays(1),
reportDate.dayOfWeek().withMaximumValue().plusDays(1)).getWeeks();
switch(day_of_week) {
case "Monday":
// Get date of this day in current week
break;
case "Tuesday":
// Get date of this day in current week
break;
// ...
You can use following code to achieve the days as desired,
LocalDateTime yourDate = LocalDateTime.now();
System.out.println(yourDate.getWeekOfWeekyear());
int weekOfyear = yourDate.getWeekOfWeekyear();
//Fetch Week Start Date for Given Week Number
DateTime weekStartDate = new DateTime().withWeekOfWeekyear(weekOfyear);
System.out.println(weekStartDate.toString());
//Fetch Specific Days for given week
DateTime wedDateTime = weekStartDate.withDayOfWeek(DateTimeConstants.WEDNESDAY);
I hope this answer your query.
As per my understanding of your requirement I have implemented below code, just check if it helps you,
First Import Calendar Package As Below
import java.util.Calendar;
Now Create below function
public String getSpecificDate(int weekOfYear, int dayOfWeek)
{
Calendar cal = Calendar.getInstance();
cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH); // 0 to 11
int day = cal.get(Calendar.DAY_OF_MONTH);
String selectedDate = " " + day + "-" + (month+1) + "-" + year;
return selectedDate;
}
// I have passed getSpecificDate(41, 6) and get 7-10-2016 as output

Get a day from week based on Unix timestamp

The Unix timestamp is 1417029117, which is 11/26/2014, Wednesday.
long timestamp = 1417029117l*1000l;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp);
System.out.println("current day is "+cal.get(Calendar.DAY_OF_WEEK));
System.out.println("current month is "+cal.get(Calendar.MONTH));
And I got the results as follows:
current day is 4
current month is 10
Any explanation? If January is 0 then the month is fine. But why the day is 4?
First day of the week is Sunday. So, Wednesday is 4. See Calendar#DAY_OF_WEEK and Constant Field Values, Calendar#WEDNESDAY, it's plain out there in the documentation.

Day and month incorrect when setting next day of date using Calendar

//fetch date and convert to date type
String DateString = Integer.toString(getDay()) + "/" + Integer.toString(getMonth()) + "/" + Integer.toString(getYear());
DateFormat parser = new SimpleDateFormat("dd/MM/yyyy"); //current format of date
Date date = (Date) parser.parse(DateString); //convert string to date
//calculate next day
Calendar cal = Calendar.getInstance();
cal.setTime(date); //set calendar time to chosen date
cal.add(Calendar.DATE, 1); //add 1 day to calendar date
//set object to next day
parser.format(cal.getTime()); //set format to dd/MM/yyyy
setDay(cal.get(cal.DAY_OF_MONTH));
setMonth(cal.get(cal.MONTH));
setYear(cal.get(cal.YEAR));
I set a date to 23 October 2002. I want to set it to the next day using the above method. It shows 24 September 2002 instead of 24 October 2002. Why is it adding 1 to the day and removing 1 from the month?
The reason is that months are zero based index ie, they start from 0 instead of 1 so January is 0, Feb is 1, march is 2 and .....Decemeber is 11
From the Oracle docs:
A month is represented by an integer from 0 to 11; 0 is January, 1 is
February, and so forth; thus 11 is December.
EDIT:-
Trying to give the reason for why months start with zero.
The tm structure which is defined in time.h has an integer field tm_mon with the range of 0-11, so I guess this has been taken from the C language. One other reason which might sound wierd but can be reason that since we have names of the month but for days(1,2,3...30,31) we dont have any names

Calendar.MONTH setting to wrong month

//this month
SimpleDateFormat df_formonth = new SimpleDateFormat("MMM");
c.set(Calendar.MONTH, 5); //integer to be changed upon click - maybe month counter from now
String currmonth = df_formonth.format(c.getTime());
This should return June since we index months from 0 to 11
but it returns july
any solutions or other ways to fix this?
Because today's date is the 31st of August and June only has 30 days, the month is automatically incremented to the following month giving July.
To solve you can set the date before setting the month
c.set(Calendar.DATE, 30);
c.set(Calendar.MONTH, Calendar.JUNE);
Also I suggest using Calendar constants for clarity
Well known issue when you are working with dates at the end of the month (31st of Aug).
You should explicitly set the date.
For example read here for details:
http://www.coderanch.com/t/385083/java/java/java-util-Calendar-set
You can try the following:
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DAY_OF_MONTH);
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.MONTH, Calendar.JUNE);

Categories

Resources