How to get a date by giving the week of the month and day of the week
How do i get a date by giving the week of the month and day of the week.
Example: If i need the third tuesday of November 2018.
Week of month : 3,
Day of Week : 3
Expected date is : Nov 20 2018
But the value we get is : Nov 13 2018. Since the start day of the month (thursday) is less than than the expected day(tuesday).
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2018 );
calendar.set(Calendar.MONTH, 10);
calendar.set(Calendar.WEEK_OF_MONTH, 3);
calendar.set(Calendar.DAY_OF_WEEK, 3);
System.out.println("Time " + calendar.getTime());
How do i get the correct date??
java date calendar dayofweek week-number
add a comment |
How do i get a date by giving the week of the month and day of the week.
Example: If i need the third tuesday of November 2018.
Week of month : 3,
Day of Week : 3
Expected date is : Nov 20 2018
But the value we get is : Nov 13 2018. Since the start day of the month (thursday) is less than than the expected day(tuesday).
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2018 );
calendar.set(Calendar.MONTH, 10);
calendar.set(Calendar.WEEK_OF_MONTH, 3);
calendar.set(Calendar.DAY_OF_WEEK, 3);
System.out.println("Time " + calendar.getTime());
How do i get the correct date??
java date calendar dayofweek week-number
If you just input the week of month and the day of week, then how do you determine which month it is?
– deHaar
Nov 21 '18 at 10:58
WEEK_OF_MONTH should be 4. Since 20th falls on 4th week of Nov. Works fine with this value. Tested.
– bitsobits
Nov 21 '18 at 11:03
TheCalendar
class has design problems and is long outdated. Usejava.time
, the modern Java date and time API. Depending on your definition of week numbers in a month theWeekFields
class may be useful.
– Ole V.V.
Nov 21 '18 at 12:32
add a comment |
How do i get a date by giving the week of the month and day of the week.
Example: If i need the third tuesday of November 2018.
Week of month : 3,
Day of Week : 3
Expected date is : Nov 20 2018
But the value we get is : Nov 13 2018. Since the start day of the month (thursday) is less than than the expected day(tuesday).
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2018 );
calendar.set(Calendar.MONTH, 10);
calendar.set(Calendar.WEEK_OF_MONTH, 3);
calendar.set(Calendar.DAY_OF_WEEK, 3);
System.out.println("Time " + calendar.getTime());
How do i get the correct date??
java date calendar dayofweek week-number
How do i get a date by giving the week of the month and day of the week.
Example: If i need the third tuesday of November 2018.
Week of month : 3,
Day of Week : 3
Expected date is : Nov 20 2018
But the value we get is : Nov 13 2018. Since the start day of the month (thursday) is less than than the expected day(tuesday).
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2018 );
calendar.set(Calendar.MONTH, 10);
calendar.set(Calendar.WEEK_OF_MONTH, 3);
calendar.set(Calendar.DAY_OF_WEEK, 3);
System.out.println("Time " + calendar.getTime());
How do i get the correct date??
java date calendar dayofweek week-number
java date calendar dayofweek week-number
edited Nov 21 '18 at 12:32
Ole V.V.
31.1k63956
31.1k63956
asked Nov 21 '18 at 10:54
AjuAju
4482927
4482927
If you just input the week of month and the day of week, then how do you determine which month it is?
– deHaar
Nov 21 '18 at 10:58
WEEK_OF_MONTH should be 4. Since 20th falls on 4th week of Nov. Works fine with this value. Tested.
– bitsobits
Nov 21 '18 at 11:03
TheCalendar
class has design problems and is long outdated. Usejava.time
, the modern Java date and time API. Depending on your definition of week numbers in a month theWeekFields
class may be useful.
– Ole V.V.
Nov 21 '18 at 12:32
add a comment |
If you just input the week of month and the day of week, then how do you determine which month it is?
– deHaar
Nov 21 '18 at 10:58
WEEK_OF_MONTH should be 4. Since 20th falls on 4th week of Nov. Works fine with this value. Tested.
– bitsobits
Nov 21 '18 at 11:03
TheCalendar
class has design problems and is long outdated. Usejava.time
, the modern Java date and time API. Depending on your definition of week numbers in a month theWeekFields
class may be useful.
– Ole V.V.
Nov 21 '18 at 12:32
If you just input the week of month and the day of week, then how do you determine which month it is?
– deHaar
Nov 21 '18 at 10:58
If you just input the week of month and the day of week, then how do you determine which month it is?
– deHaar
Nov 21 '18 at 10:58
WEEK_OF_MONTH should be 4. Since 20th falls on 4th week of Nov. Works fine with this value. Tested.
– bitsobits
Nov 21 '18 at 11:03
WEEK_OF_MONTH should be 4. Since 20th falls on 4th week of Nov. Works fine with this value. Tested.
– bitsobits
Nov 21 '18 at 11:03
The
Calendar
class has design problems and is long outdated. Use java.time
, the modern Java date and time API. Depending on your definition of week numbers in a month the WeekFields
class may be useful.– Ole V.V.
Nov 21 '18 at 12:32
The
Calendar
class has design problems and is long outdated. Use java.time
, the modern Java date and time API. Depending on your definition of week numbers in a month the WeekFields
class may be useful.– Ole V.V.
Nov 21 '18 at 12:32
add a comment |
3 Answers
3
active
oldest
votes
If you are using Java 8, you can do this,
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String args) {
LocalDate input = LocalDate.now();
int ordinal = 3;
DayOfWeek weekday = DayOfWeek.THURSDAY;
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth(ordinal, weekday) ;
LocalDate adjusted = input.with( ta );
System.out.println(adjusted.toString());
}
}
I have taken it from here, and there are more ways to do that in the given link.
1
For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.
– Basil Bourque
Nov 21 '18 at 18:01
add a comment |
You can use the following code :
LocalDate.now().with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.TUESDAY));
This will output :
2018-11-20
More info can be found in the java docs here
add a comment |
If you want to use Calendar then:
int year = 2018;
int month = 10; // zero based November
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
System.out.println(calendar.getTime());
These terrible old classes were supplanted years ago by the java.time classes.
– Basil Bourque
Nov 21 '18 at 17:57
1
I know, this is why I prefix my answer: If you want to use Calendar then. The other answers cover the modern way.
– forpas
Nov 21 '18 at 17:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410568%2fhow-to-get-a-date-by-giving-the-week-of-the-month-and-day-of-the-week%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you are using Java 8, you can do this,
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String args) {
LocalDate input = LocalDate.now();
int ordinal = 3;
DayOfWeek weekday = DayOfWeek.THURSDAY;
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth(ordinal, weekday) ;
LocalDate adjusted = input.with( ta );
System.out.println(adjusted.toString());
}
}
I have taken it from here, and there are more ways to do that in the given link.
1
For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.
– Basil Bourque
Nov 21 '18 at 18:01
add a comment |
If you are using Java 8, you can do this,
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String args) {
LocalDate input = LocalDate.now();
int ordinal = 3;
DayOfWeek weekday = DayOfWeek.THURSDAY;
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth(ordinal, weekday) ;
LocalDate adjusted = input.with( ta );
System.out.println(adjusted.toString());
}
}
I have taken it from here, and there are more ways to do that in the given link.
1
For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.
– Basil Bourque
Nov 21 '18 at 18:01
add a comment |
If you are using Java 8, you can do this,
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String args) {
LocalDate input = LocalDate.now();
int ordinal = 3;
DayOfWeek weekday = DayOfWeek.THURSDAY;
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth(ordinal, weekday) ;
LocalDate adjusted = input.with( ta );
System.out.println(adjusted.toString());
}
}
I have taken it from here, and there are more ways to do that in the given link.
If you are using Java 8, you can do this,
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String args) {
LocalDate input = LocalDate.now();
int ordinal = 3;
DayOfWeek weekday = DayOfWeek.THURSDAY;
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth(ordinal, weekday) ;
LocalDate adjusted = input.with( ta );
System.out.println(adjusted.toString());
}
}
I have taken it from here, and there are more ways to do that in the given link.
edited Nov 21 '18 at 18:00
Basil Bourque
115k30394557
115k30394557
answered Nov 21 '18 at 11:01
SandSand
1,7213822
1,7213822
1
For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.
– Basil Bourque
Nov 21 '18 at 18:01
add a comment |
1
For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.
– Basil Bourque
Nov 21 '18 at 18:01
1
1
For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.
– Basil Bourque
Nov 21 '18 at 18:01
For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.
– Basil Bourque
Nov 21 '18 at 18:01
add a comment |
You can use the following code :
LocalDate.now().with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.TUESDAY));
This will output :
2018-11-20
More info can be found in the java docs here
add a comment |
You can use the following code :
LocalDate.now().with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.TUESDAY));
This will output :
2018-11-20
More info can be found in the java docs here
add a comment |
You can use the following code :
LocalDate.now().with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.TUESDAY));
This will output :
2018-11-20
More info can be found in the java docs here
You can use the following code :
LocalDate.now().with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.TUESDAY));
This will output :
2018-11-20
More info can be found in the java docs here
answered Nov 21 '18 at 11:01
Nicholas KNicholas K
8,14661638
8,14661638
add a comment |
add a comment |
If you want to use Calendar then:
int year = 2018;
int month = 10; // zero based November
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
System.out.println(calendar.getTime());
These terrible old classes were supplanted years ago by the java.time classes.
– Basil Bourque
Nov 21 '18 at 17:57
1
I know, this is why I prefix my answer: If you want to use Calendar then. The other answers cover the modern way.
– forpas
Nov 21 '18 at 17:59
add a comment |
If you want to use Calendar then:
int year = 2018;
int month = 10; // zero based November
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
System.out.println(calendar.getTime());
These terrible old classes were supplanted years ago by the java.time classes.
– Basil Bourque
Nov 21 '18 at 17:57
1
I know, this is why I prefix my answer: If you want to use Calendar then. The other answers cover the modern way.
– forpas
Nov 21 '18 at 17:59
add a comment |
If you want to use Calendar then:
int year = 2018;
int month = 10; // zero based November
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
System.out.println(calendar.getTime());
If you want to use Calendar then:
int year = 2018;
int month = 10; // zero based November
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
System.out.println(calendar.getTime());
answered Nov 21 '18 at 11:06
forpasforpas
17.8k3728
17.8k3728
These terrible old classes were supplanted years ago by the java.time classes.
– Basil Bourque
Nov 21 '18 at 17:57
1
I know, this is why I prefix my answer: If you want to use Calendar then. The other answers cover the modern way.
– forpas
Nov 21 '18 at 17:59
add a comment |
These terrible old classes were supplanted years ago by the java.time classes.
– Basil Bourque
Nov 21 '18 at 17:57
1
I know, this is why I prefix my answer: If you want to use Calendar then. The other answers cover the modern way.
– forpas
Nov 21 '18 at 17:59
These terrible old classes were supplanted years ago by the java.time classes.
– Basil Bourque
Nov 21 '18 at 17:57
These terrible old classes were supplanted years ago by the java.time classes.
– Basil Bourque
Nov 21 '18 at 17:57
1
1
I know, this is why I prefix my answer: If you want to use Calendar then. The other answers cover the modern way.
– forpas
Nov 21 '18 at 17:59
I know, this is why I prefix my answer: If you want to use Calendar then. The other answers cover the modern way.
– forpas
Nov 21 '18 at 17:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410568%2fhow-to-get-a-date-by-giving-the-week-of-the-month-and-day-of-the-week%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you just input the week of month and the day of week, then how do you determine which month it is?
– deHaar
Nov 21 '18 at 10:58
WEEK_OF_MONTH should be 4. Since 20th falls on 4th week of Nov. Works fine with this value. Tested.
– bitsobits
Nov 21 '18 at 11:03
The
Calendar
class has design problems and is long outdated. Usejava.time
, the modern Java date and time API. Depending on your definition of week numbers in a month theWeekFields
class may be useful.– Ole V.V.
Nov 21 '18 at 12:32