1

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 !

Kanagalingam
  • 53
  • 1
  • 10
  • 1
    Can you use the new Java 8 time classes? See the [official date time tutorial](http://docs.oracle.com/javase/tutorial/datetime/index.html). `GregorianCalendar` can be made to work, but it’s old stuff now, and the new classes are generally easier to work with, not least for tasks like this one. – Ole V.V. Dec 16 '16 at 11:30

6 Answers6

1

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

mustafa918
  • 498
  • 1
  • 6
  • 15
1

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

1

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
Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124
  • I tried your code sissy! It works fine for the date 16/11/2016. But if i change the date as ( 2016, 12, 16 ), it shows o/p as Mon Jan 23 00:00:00 GMT+05:30 2017...., The o/p has to be Mon Dec 19 (19/12/2016) na..... – Kanagalingam Dec 16 '16 at 12:00
  • 1
    @Kanagalingam `( 2016, 12, 16 )` is the 13th month (read again: 13th) of 2016. The month must be a value between 0 and 11 included. It's weird but that's how old Date API works: with month starting from zero. Also please don't call me Sissy. – Margaret Bloom Dec 16 '16 at 12:11
  • Thanks for your reply Margaret Bllom :P Now i understood the concept ! – Kanagalingam Dec 16 '16 at 12:28
1

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
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Just to note, this question is about Android where these old classes are still standard and NOT legacy, so you should at least emphasize rather your favourite ThreetenABP and not the Oracle Java-8-version if you want to advise Android developers to NOT use the standard. By the way, recommending 3rd-party libraries like Threeten-Extra which cannot even run on Android is not helpful and completely off topic. – Meno Hochschild Dec 17 '16 at 15:48
0

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.

Alexander
  • 1,356
  • 9
  • 16
0

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);
Kanagalingam
  • 53
  • 1
  • 10