-2

This is my Java code to find the current month/date we are in:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GetCurrentDateTime {

 private static final DateFormat sdf = new SimpleDateFormat("MM-dd");


public static void main(String[] args) {

    Date date = new Date();
    System.out.println(sdf.format(date));

     }
}

I want this code to do something else. When the code finds the month we are in, I also want it to find the month length. Result should be like this:

08-16
31

Can you help me with this? Thank you.

jansetk
  • 29
  • 3
  • 2
    Sure. We would love to help you. But only when we see that you have already tried your best to solve this problem. – akash Aug 16 '17 at 06:34
  • @TAsk That is not a requirement for all Questions on Stack Overflow. – Basil Bourque Aug 16 '17 at 06:36
  • 4
    Possible duplicate of [Number of days in particular month of particular year?](https://stackoverflow.com/questions/8940438/number-of-days-in-particular-month-of-particular-year) – Lemonov Aug 16 '17 at 06:37
  • I tried my best. I know that asking stackoverflow is not the first way to solve problems. I tried my best, I searched for my problems., but I couldn't see the result. So I am asking to you. I don't understand why experienced users act that strict to newbies. – jansetk Aug 16 '17 at 06:40
  • 1
    we want to **see** your best. Post it here and we can inform you how to fix it – Scary Wombat Aug 16 '17 at 06:41
  • @jansetk Have a look at my solution – mohit sharma Aug 16 '17 at 07:13

4 Answers4

5

tl;dr

YearMonth.now()
         .lengthOfMonth()

Details

Avoid the troublesome old date-time classes seen in the Question. Now supplanted by the java.time classes.

A time zone is crucial in determining today's date and therefore in determining the current month. For any given moment the date varies around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
YearMonth ym = YearMonth.now( z ) ;
int daysInMonth = ym.lengthOfMonth() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

You could try something like this, but I think, there might be a better way:

Date date = new Date();
System.out.println(sdf.format(date));
Calendar c = Calendar.getInstance();
c.setTime(date);
System.out.println(c.getActualMaximum(Calendar.DAY_OF_MONTH));

Output is this:

08-16
31

EDIT:
As written in the Comments:

Use the solution from Basil YearMonth.now().lengthOfMonth(), it's a better way than this

StefanPG
  • 155
  • 3
  • 10
  • Use the solution from Basil `YearMonth.now().lengthOfMonth()`, it's a better way than this. – StefanPG Aug 16 '17 at 06:44
  • Really, it's time to put that `Calendar` class in a box and tuck it away at the back of the attic to be ignored and forgotten. – Basil Bourque Aug 16 '17 at 07:15
  • Shame on me, I didn't even know about ZoneId/YearMonth when I was writing my solution. But you are right, it's a better and clearer way. – StefanPG Aug 16 '17 at 07:27
0

Maybe that:

   @Test
    public void daysOfMonthTest() throws Exception {
    Calendar mycal = new GregorianCalendar();

    assertEquals(
            31,
            mycal.getActualMaximum(Calendar.DAY_OF_MONTH));
    }
Minh Tuan Nguyen
  • 1,026
  • 8
  • 13
0

Note: The Months are numbered from 0 (January) to 11 (December).

Using Simple Date Object:

        Date date = new Date();
        int month = date.getMonth() + 1;
        int year = date.getYear()+1900;
        System.out.print(month+"-"+year);

Output:

 8-2017

Using Calendar Object:

     Date date = new Date(); // your date
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH) + 1;
    System.out.print(month+"-"+year);
    System.out.print("Number of days: "+cal.getActualMaximum(Calendar.DAY_OF_MONTH));

Output:

8-2017
Number of days: 31

Using JodaTime:

    DateTime  dateTime = new DateTime();
    System.out.print(dateTime.getMonthOfYear()+"-"+(dateTime.getYear()));
    System.out.print("Number of days: "+ dateTime.dayOfMonth().getMaximumValue());

Output:

8-2017
Number of days: 31
mohit sharma
  • 1,050
  • 10
  • 20