简体   繁体   中英

How to get the next week monday date from user input date in java

I am a School student learning java newly!

My scenario is: * Get the input date from user * Show the next week's monday date as o/p to the user!

For example, Input Date: 15/12/2016 Output : 19/12/2016(Monday)

I have searched the forums and i have got the below code to run on.

GregorianCalendar date1 = new GregorianCalendar( 2016, 12, 12 ); 

        while( date1.get( Calendar.DAY_OF_WEEK ) != Calendar.MONDAY )
            date1.add( Calendar.DATE, 1 );

        System.out.println(date1.getTime());

But it gives me o/p as Mon Jan 16 00:00:00 GMT+05:30 2017, for 12/12/2016 i/p.

I want to get o/p as 19/12/2016. Kindly help me tech geniuses !

Use SimpleDateFormat to format your date

take care about GregorianCalendar mounth it start from 0

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY");
System.out.println(sdf.format(date1.getTime()));

it well show you that GregorianCalendar( 2016, 12, 12 ) mean

12/01/2017

Your code is working proper. You have to take month as 0 - 11 (Jan - Dec). Please try following modified code :

GregorianCalendar date1 = new GregorianCalendar( 2016, 11, 13 );  // here month start for 0 to 11 (Jan to Dec.)

    while( date1.get( Calendar.DAY_OF_WEEK ) != Calendar.MONDAY )
        date1.add( Calendar.DATE, 1 );

    System.out.println(date1.getTime());                

Thanks...

You can simplify your code:

GregorianCalendar date1 = new GregorianCalendar( 2016, 11, 16 ); 


date1.add(Calendar.DATE, 7);                       //Move to next weed
date1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  //Set the day to Monday of current week

tl;dr

LocalDate.parse( 
    "15/12/2016" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) 
).with(
    TemporalAdjusters.next( DayOfWeek.MONDAY )
)

java.time

You are using troublesome old date-time classes, now legacy, supplanted by java.time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = LocalDate.parse( "15/12/2016" , DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) );

The java.time classes use immutable objects . So rather than change (“mutate”) the value in an existing object, we instantiate a new object based on the original's values. One way to do this is with an implementation of a TemporalAdjuster .

LocalDate nextMonday = ld.with( TemporalAdjusters.next( DayOfWeek.MONDAY ) ) ;

The classes have been discussed many times. So search Stack Overflow for more info.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport .
  • Android

If you take a look in the JavaDoc of GregorianCalendar , you may see that the month parameter of the constructor you used is starting with 0 for January and so has 11 for December.

It then interprets 12 as January of 2017 , which means 16/01/2017 is the proper result.

thanks for all your help! After a hour of coding i achieved it through the below code! Kindly please go through the code and intimate me if am out of my path somewhere!

 GregorianCalendar date1 = new GregorianCalendar( year, month-1, date );
        SimpleDateFormat dateOnly = new SimpleDateFormat("dd/MM/yyyy");
//        System.out.println("Formated date"+dateOnly.format(date1.getTime()));
        String day1="",day2;

    while( date1.get( Calendar.DAY_OF_WEEK ) != Calendar.MONDAY )
        date1.add( Calendar.DATE, 1 );

    da1=String.valueOf(dateOnly.format(date1.getTime()));
    System.out.println("Start_date"+da1);

    date1.add(Calendar.DATE, 6);
    da2=String.valueOf(dateOnly.format(date1.getTime()));
    System.out.println("End_date"+da2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM