[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

determining month number and year number

Hi

I need to calculate the current month as a number and the current year as a two digit number.

for example, at the moment the values would be

int intMonth=10;
int intYear=04


Is there a function which alows me to get the month and year numbers form a timestamp?
0
rosshind
Asked:
rosshind
  • 8
  • 5
  • 4
  • +2
1 Solution
 
zzynxSoftware engineerCommented:
use SimpleDateFormat
0
 
objectsCommented:
Use Calendar class, or SimpleDateFormat.
0
 
suprapto45Commented:
Yes, use as what objects and zzynx said. Let me give you some codes.

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
zzynxSoftware engineerCommented:
Or Calendar

Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);

if (year<2000)
   year -= 1900;
else if (year>2000)
   year -= 2000;
0
 
objectsCommented:
SimpleDateFormat sdf = new SimpleDateFormat("MM");
String month = sdf.format(date);
0
 
objectsCommented:
SimpleDateFormat sdf = new SimpleDateFormat("yy");
String year = sdf.format(date);
0
 
zzynxSoftware engineerCommented:
SimpleDateFormat sdf = new SimpleDateFormat("yy");
int y = Integer.valueOf( sdf.format(new Date()) );
sdf = new SimpleDateFormat("MM");
int month = Integer.valueOf( sdf.format(new Date()) );
0
 
objectsCommented:
or using a calendar

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
0
 
objectsCommented:
which you use depends on whether you want it zero filled or not.
0
 
zzynxSoftware engineerCommented:
Sorry:

Integer y = Integer.valueOf( sdf.format(new Date()) );
Integer month = Integer.valueOf( sdf.format(new Date()) );

and user intValue() if you want the int value
0
 
suprapto45Commented:
       Calendar cal = Calendar.getInstance();
        int month = cal.getTime().getMonth();
        int year = cal.getTime().getYear();
        year += 1900;
        month++;
        System.out.println(year);
        System.out.println(month);
0
 
zzynxSoftware engineerCommented:
>>I need to calculate the **current** month as a number and the **current** year as a two digit number.

The Calendar returned by getInstance is based on the **current time** in the default time zone with the default locale.

So, after

        Calendar cal = Calendar.getInstance();
no
        cal.setTime(...);

is needed in your case
0
 
suprapto45Commented:
Why do you need month++?
The first month of the year is JANUARY which is 0; the last depends on the number of months in a year.

For the year, it is meant to do so :)
0
 
zzynxSoftware engineerCommented:
suprato,

1) getYear() and getMonth() are deprecated functions of the Date class
2) he wants the year in two digits
0
 
suprapto45Commented:
Oh yes.....:). Thank you for reminding
0
 
shah1d1698Commented:
objects and zzynx have already given you the way to solve your problem. If you need more help, just compile & run the following code.

import java.util.*;

class ABC
{
  public static void main(String arguments[])
  {
      Date date = new Date(System.currentTimeMillis());
      System.out.println(date);
      //this will print the current date and time

      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      int month = cal.get(cal.MONTH);
      month++;

  /*Note that month value is incremented by one. This is done to be consistent with the convention that we use in expressing month in a date. Here cal.get(cal.MONTH) returns a value between 0(January) and 11(December). This is 13th October and so this mehtod will return 9 for the month October. But to express today's date in the format of mm/dd/yyyy, we use 10/13/2004. The value of the variable month is incremented so that month can be expressed in numerals in the range of 1 to 12. */
      System.out.println("Month : "+month);
      int year = cal.get(cal.YEAR);
      System.out.println("Year : "+year);
  }
}
0
 
zzynxSoftware engineerCommented:
Any questions left?
0
 
zzynxSoftware engineerCommented:
Thanks for passing by and accepting ;°)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 8
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now