Solved

determining month number and year number

Posted on 2004-10-13
19
261 Views
Last Modified: 2010-03-31
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
Comment
Question by:rosshind
  • 8
  • 5
  • 4
  • +2
19 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12295912
use SimpleDateFormat
0
 
LVL 92

Expert Comment

by:objects
ID: 12295913
Use Calendar class, or SimpleDateFormat.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12295918
Yes, use as what objects and zzynx said. Let me give you some codes.

0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 12295920
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
 
LVL 92

Expert Comment

by:objects
ID: 12295925
SimpleDateFormat sdf = new SimpleDateFormat("MM");
String month = sdf.format(date);
0
 
LVL 92

Expert Comment

by:objects
ID: 12295932
SimpleDateFormat sdf = new SimpleDateFormat("yy");
String year = sdf.format(date);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12295939
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
 
LVL 92

Expert Comment

by:objects
ID: 12295941
or using a calendar

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
0
 
LVL 92

Expert Comment

by:objects
ID: 12295944
which you use depends on whether you want it zero filled or not.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Expert Comment

by:zzynx
ID: 12295945
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 12295967
       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
 
LVL 37

Expert Comment

by:zzynx
ID: 12295974
>>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
 
LVL 16

Expert Comment

by:suprapto45
ID: 12295982
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12295988
suprato,

1) getYear() and getMonth() are deprecated functions of the Date class
2) he wants the year in two digits
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12296067
Oh yes.....:). Thank you for reminding
0
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12299173
0
 
LVL 2

Expert Comment

by:shah1d1698
ID: 12299859
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12308321
Any questions left?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12544793
Thanks for passing by and accepting ;°)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running Jira on Raspberry PI 2? 3 247
PDFBox - convert image to PDF, PDF resolution 3 112
Groovy:unable to resolve class error 2 66
Cipher Configuration on Apache HTTPD 4 50
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now