Calendar - Dates as per Week - Java - java

public static void main(String[] args) {
int week = 1;
int year = 2010;
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.WEEK_OF_YEAR, week);
calendar.set(Calendar.YEAR, year);
Date date = calendar.getTime();
System.out.println(date);
}
I'm looking for the exact start and end DATE's as per our desktop calendars if I give week, year as input.
But the above code is giving the output as 27th Jan, 2009, Sunday.
I know it's because the default first day of a week is SUNDAY as per US, but I need as per the desktop calendar 1st Jan, 2010, Friday as starting date of the week
My Requirement :
If my input is :
Week as '1',
Month as '5',
Year as '2015'
I need :
1st May, 2015 --> as first day of the week
2nd May, 2015 --> as last day of the week
If my input is :
Week as '1',
Month as '6',
Year as '2015'
I need :
1st June, 2015 --> as first day of the week
6th June, 2015 --> as last day of the week
Can anyone help me?

Instead of using CALENDAR.Week, use Calendar.DAY_OF_YEAR. I just tested it and it works for me:
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, 2010);
calendar.set(Calendar.DAY_OF_YEAR, 1);
System.out.println(calendar.getTime());
calendar.set(Calendar.DAY_OF_YEAR, 7);
System.out.println(calendar.getTime());
}
If you want this to work for an arbitrary week, just do some math to figure out which day of the year you want.
Edit: If you want to input a month as well, you can use Calendar.DAY_OF_MONTH.

I wrote a Swing calendar widget. One method in that widget calculates the first day of the week when a week starts on a user selected day, like Friday.
startOfWeek is an int that takes a Calendar constant, like Calendar.FRIDAY.
DAYS_IN_WEEK is an int constant with the value 7.
/**
* This method gets the date of the first day of the calendar week. It could
* be the first day of the month, but more likely, it's a day in the
* previous month.
*
* #param calendar
* - Working <code>Calendar</code> instance that this method can
* manipulate to set the first day of the calendar week.
*/
private void getFirstDate(Calendar calendar) {
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) % DAYS_IN_WEEK;
int amount = 0;
for (int i = 0; i < DAYS_IN_WEEK; i++) {
int j = (i + startOfWeek) % DAYS_IN_WEEK;
if (j == dayOfWeek) {
break;
}
amount--;
}
calendar.add(Calendar.DAY_OF_MONTH, amount);
}
The rest of the code can be seen in my article, Swing JCalendar Component.

Related

Get start date of current financial year

Here in the UK, the tax year runs from 6 April to 5 April each year. I want to get the start date of the current tax year (as a LocalDate), so for example if today is 3 April 2020, then return 6 April 2019, and if today is 8 April 2020, then return 6 April 2020.
I can calculate it using some logic like the following:
date = a new LocalDate of 6 April with today's year
if (the date is after today) {
return date minus 1 year
} else {
return date
}
But is there some method I can use that is less complex and uses a more succinct, perhaps functional style?
There are a few different approaches, but it's easy enough to implement the logic you've already specified in a pretty functional style:
private static final MonthDay FINANCIAL_START = MonthDay.of(4, 6);
private static LocalDate getStartOfFinancialYear(LocalDate date) {
// Try "the same year as the date we've been given"
LocalDate candidate = date.with(FINANCIAL_START);
// If we haven't reached that yet, subtract a year. Otherwise, use it.
return candidate.isAfter(date) ? candidate.minusYears(1) : candidate;
}
That's pretty concise and simple. Note that it doesn't use the current date - it accepts a date instead. That makes it much easier to test. It's easy enough to call this and provide the current date, of course.
using java.util.Calendar, you can get financial year's START and END date in which your given date lies.
In India financial year starts from from 1 April and ends on 31st March,
for financial year 2020-21 , dates will be 1 April 2020
public static Date getFirstDateOfFinancialYear(Date dateToCheck) {
int year = getYear(dateToCheck);
Calendar cal = Calendar.getInstance();
cal.set(year, 3, 1); // 1 April of Year
Date firstAprilOfYear = cal.getTime();
if (dateToCheck.after(firstAprilOfYear)) {
return firstAprilOfYear;
} else {
cal.set(year - 1, 3, 1);
return cal.getTime();
}
}
In your case set cal.set(year, 0, 1); // 1 Jan of Year
public static Date getLastDateOfFinancialYear(Date dateToCheck) {
int year = getYear(dateToCheck);
Calendar cal = Calendar.getInstance();
cal.set(year, 2, 31); // 31 March of Year
Date thirtyFirstOfYear = cal.getTime();
if (dateToCheck.after(thirtyFirstOfYear)) {
cal.set(year + 1, 2, 31);
return cal.getTime();
} else {
return thirtyFirstOfYear;
}
}
In your case set cal.set(year, 11, 31); // 31 Dec of Year

how to get last thursday of month in java [duplicate]

This question already has answers here:
Get Last Friday of Month in Java
(16 answers)
Closed 4 years ago.
I have to fetch date of last Thursday of month of any year but the problem I'm facing is that for the month of dec'15 last thursday is 31-dec-2015 but I'm getting 24-dec-2015 for the following code:
Date getLastThursday(def month, def year ) {
Calendar cal = Calendar.getInstance();
cal.set( year, month,1 )
cal.add( Calendar.DAY_OF_MONTH, -(cal.get(Calendar.DAY_OF_WEEK )%7+2) );
return cal.getTime();
}
And also explain me how this line of code internally works?
cal.add(Calendar.DAY_OF_MONTH, -(cal.get(Calendar.DAY_OF_WEEK )%7+2))
If you use Java 8+, you can use a temporal adjuster (part of the Java Time API):
int month = 12;
int year = 2015;
LocalDate lastThursday = LocalDate.of(year, month, 1).with(lastInMonth(THURSDAY));
System.out.println("lastThursday = " + lastThursday); //prints 2015-12-31
Note: requires static imports
import static java.time.DayOfWeek.THURSDAY;
import static java.time.temporal.TemporalAdjusters.lastInMonth;
If you can't use the new API, I suspect the problem is in the modulus operation and this should work:
//month should be 0-based, i.e. use 11 for December
static Date getLastThursday(int month, int year) {
Calendar cal = Calendar.getInstance();
cal.set(year, month + 1, 1);
cal.add(Calendar.DAY_OF_MONTH, -((cal.get(Calendar.DAY_OF_WEEK) + 2) % 7));
if (cal.get(Calendar.MONTH) != month) cal.add(Calendar.DAY_OF_MONTH, -7);
return cal.getTime();
}
The second if condition is there to make sure we have gone back one month.
Just put your modulus % outside:
cal.add( Calendar.DAY_OF_MONTH, -(cal.get(Calendar.DAY_OF_WEEK )+2)%7 );
if not, you get -8 => 24 december
and use month+1

Finding out what day the first of any given month is

So I need to make a basic calendar which displays the calendar for a specific year and month (the user should be able to select
any month and any year based on text input).
So far, I have managed to create a calender obj, use Scanner to get the desired month and year from the user but my question is that how do I find out what the first day of the month is? In the example above, it's a Saturday. My logic to building it is that if I know the first day, I can make a String[][] array and start displaying the day on the relevant date by looping through the month from the first day. I've used a scanner to get the required month and year. I then created a calendar object and set the Calendar variables; Calendar.YEAR and Calendar.MONTH, as per required by the user:
Calendar cal= Calendar.getInstance();
cal.set(Calendar.YEAR, year); //my year variable is set 2015
cal.set(Calendar.MONTH, chosenMonth); //my chosenMonth is set to 5 since January starts from 0.
I tried using the following code to test out my calender to see if it will execute the code if Monday was the first day of the month. On june 2015, it was.
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
System.out.println("This will print is my calendar secessfully gathers that monday was the first day of the month on June 2015.")
}
It doesn't execute.
Try this:
cal.set(Calendar.DATE, 1);
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEEE");
System.out.println(sdf.format(cal.getTime()));
Output:
Saturday
Demo
Java SE 8 has a whole new API for date and time, java.time. See Tutorial.
You can use the class LocalDate to get the day of a given week. For example, using your day from above do,
LocalDate d = LocalDate.of(2011, 10, 1);
Then LocalDate::getDayOfWeek() method will return a DayOfWeek enum instance, such as SATURDAY.
DayOfWeek dayOfWeek = localDate.getDayOfWeek ();
That enum can render a localized string for your sentence output.
String output = dayOfWeek.getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH); // Or Locale.US or Locale.ENGLISH, and so on.
samedi

How to get all the dates in a month using calender class?

Here I want to display dates like
2013-01-01,
2013-01-02,
2013-01-03,
.
.
...etc
I can get total days in a month
private int getDaysInMonth(int month, int year) {
Calendar cal = Calendar.getInstance(); // or pick another time zone if necessary
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 1); // 1st day of month
cal.set(Calendar.YEAR, year);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
Date startDate = cal.getTime();
int nextMonth = (month == Calendar.DECEMBER) ? Calendar.JANUARY : month + 1;
cal.set(Calendar.MONTH, nextMonth);
if (month == Calendar.DECEMBER) {
cal.set(Calendar.YEAR, year + 1);
}
Date endDate = cal.getTime();
// get the number of days by measuring the time between the first of this
// month, and the first of next month
return (int)((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
}
Does anyone have an idea to help me?
If you only want to get the max number of days in a month you can do the following.
// Set day to one, add 1 month and subtract a day
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.get(Calendar.DAY_OF_MONTH);
If you actually want to print every day then you can just set the day of month to 1 and keep adding a day in a loop until the month changes.
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
int myMonth=cal.get(Calendar.MONTH);
while (myMonth==cal.get(Calendar.MONTH)) {
System.out.print(cal.getTime());
cal.add(Calendar.DAY_OF_MONTH, 1);
}
Modern answer: Don’t use Calendar. Use java.time, the modern Java date and time API.
YearMonth ym = YearMonth.of(2013, Month.JANUARY);
LocalDate firstOfMonth = ym.atDay(1);
LocalDate firstOfFollowingMonth = ym.plusMonths(1).atDay(1);
firstOfMonth.datesUntil(firstOfFollowingMonth).forEach(System.out::println);
Output (abbreviated):
2013-01-01
2013-01-02
2013-01-03
…
2013-01-30
2013-01-31
datesUntil gives us a stream of dates until the specified end date exclusive, so when we give it the 1st of the following month, we get exactly all the dates of the month in question. In this example case up to and including January 31.
Link: Oracle tutorial: Date Time explaining how to use java.time.
This will give you all days of a month.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.print(df.format(cal.getTime()));
for (int i = 1; i < maxDay; i++) {
cal.set(Calendar.DAY_OF_MONTH, i + 1);
System.out.print(", " + df.format(cal.getTime()));
}
The first date is printed outside of loop for comma separated output.
A couple of comments...
Firstly, "... Calendar objects are particularly expensive to create." (J. Bloch, Effective Java, 2nd Ed.). If this is a method that you are going to be calling frequently, consider that you do not need to create a new Calendar object every time you call it.
Consider using a Calendar object held in a private static field that is initialized with a static initializer block. This presumes a single-threaded solution and would require synchronization in a concurrent environment. Otherwise, it really ought to be possible to reuse the same Calendar for your calculations.
Secondly, while you can find that greatest value for the DAY_OF_MONTH by iterating over the possible valid values, I think you can let the API do it for you. Consider using the getMaximum(DAY_OF_MONTH) or getGreatestMaximum(DAY_OF_MONTH) methods of the Calendar class.
Write a common method like that if you are using kotlin-
fun getAllDateOfMonth(year: Int, month: Month): List<LocalDate> {
val yearMonth= YearMonth.of(year, month)
val firstDayOfTheMonth = yearMonth.atDay(1)
val datesOfThisMonth = mutableListOf<LocalDate>()
for (daysNo in 0 until yearMonth.lengthOfMonth()){
datesOfThisMonth.add(firstDayOfTheMonth.plusDays(daysNo.toLong()))
}
return datesOfThisMonth
}
And call it like that -
getAllDateOfMonth(2021,Month.MAY):

Android Calendar: Changing the start day of week

i have a little problem, i'm developing an application, and i need to change the start day of the week from monday to another one (thursday, of saturday). is this possible in android,
i need to calculate the start to week and its end knowing the date. (the week starts ano thursday as example)
Note: i'm just a beginner in android development.
here is my code
SimpleDateFormat dateformate = new SimpleDateFormat("dd/MM");
// get today and clear time of day
Calendar cal = Calendar.getInstance();
// get start of this week in milliseconds
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
cal.add(Calendar.DAY_OF_YEAR, 7*(WeekIndex-1));
result = dateformate.format(cal.getTime());
cal.add(Calendar.DAY_OF_YEAR, 6 );
result=result+" - " + dateformate.format(cal.getTime());
using the above code im getting the result but with monday as the star of week.
Note: i can't add day to the result because week index changes with the changing of it's start
Calendar days have values 1-7 for days Sunday-Saturday. getFirstDayOfWeek returns one of this values (usually of Monday or Sunday) depending on used Locale. Calendar.getInstance uses default Locale depening on phone's settings, which in your case has Monday as first day of the week.
One solution would be to use other Locale:
Calendar.getInstance(Locale.US).getFirstDayOfWeek()
would return 1, which is value of Calendar.SUNDAY
Other solution would be to use chosen day of week value like
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
Problem is, Calendar is using its inner first day of the week value in set as well. Example:
Calendar mondayFirst = Calendar.getInstance(Locale.GERMANY); //Locale that has Monday as first day of week
mondayFirst.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
log(DateUtils.formatDateTime(context, mondayFirst.getTimeInMillis(), 0));
//prints "May 19" when runned on May 13
Calendar sundayFirst = Calendar.getInstance(Locale.US); //Locale that has Sunday as first day of week
sundayFirst.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
log(DateUtils.formatDateTime(context, sundayFirst.getTimeInMillis(), 0));
//prints "May 12" when runned on May 13
If you don't want to use Locale or you need other day as the first day of the week, it may be best to calculate start of the week on your own.
GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 0);
changing the value 0 - starts day from monday
changing the value 1 - starts day from sunday
and so on..
hope this helps and works :)
public int getWeekdayOfMonth(int year, int month){
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, 1);
dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
return dayOfWeek;
}
weekday = getWeekdayOfMonth();
int day = (weekday - firstweek) < 0 ? (7 - (firstweek - weekday)) : (weekday - firstweek);
"firstweek" means what the start day of you want
then you can calculate the first day you should show.If you have simple method,please tell us. thks
Problem in my case was using Calendar instance returned by MaterialDialog DatePicker, which although having the same Locale as my Calendar.getInstance(Locale...), was having different Calendar.firstDayOfWeek. If you're experiencing the same issue, my workaround was to create new instance of Calendar with my Locale and just changing the property time to the one returned by the DatePicker as following:
val correctCal = Calendar.getInstance(Locale...)?.apply {
time = datePickerCal.time
}
This should return proper Calendar.firstDayOfWeek based on your Locale.

Categories