How to get a date by giving the week of the month and day of the week












0















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??










share|improve this question

























  • 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. 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
















0















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??










share|improve this question

























  • 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. 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














0












0








0








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??










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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













  • 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



















  • 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. 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

















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












3 Answers
3






active

oldest

votes


















3














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.






share|improve this answer





















  • 1





    For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.

    – Basil Bourque
    Nov 21 '18 at 18:01



















2














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






share|improve this answer































    2














    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());





    share|improve this answer
























    • 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











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    3














    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.






    share|improve this answer





















    • 1





      For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.

      – Basil Bourque
      Nov 21 '18 at 18:01
















    3














    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.






    share|improve this answer





















    • 1





      For Java 6 & 7, this functionality can be found in the ThreeTen-Backport project.

      – Basil Bourque
      Nov 21 '18 at 18:01














    3












    3








    3







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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














    • 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













    2














    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






    share|improve this answer




























      2














      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






      share|improve this answer


























        2












        2








        2







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 11:01









        Nicholas KNicholas K

        8,14661638




        8,14661638























            2














            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());





            share|improve this answer
























            • 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
















            2














            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());





            share|improve this answer
























            • 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














            2












            2








            2







            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());





            share|improve this answer













            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());






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?