-
Ho do I fix Gregorian Calender Displaying wring date
Hi the code below will result in this being printed to screen:
Today Year: 2005
Today Month: 8
Today Day: 13
Today Hour: 8
Today Minute: 59
Today date: 2005-8-13
Yet the actual date is 2005-9-13. Can anyone tell me why the month keeps comeing out wrong?
import java.util.*;
import java.util.Date;
import java.text.*;
import java.text.DateFormat;
public class dates{
public static void main(String[] args){
Calendar today = Calendar.getInstance();
System.out.println("Today Year: " + today.get(Calendar.YEAR));
System.out.println("Today Month: " + today.get(Calendar.MONTH));
System.out.println("Today Day: " + today.get(Calendar.DAY_OF_MONTH));
System.out.println("Today Hour: " + today.get(Calendar.HOUR));
System.out.println("Today Minute: " + today.get(Calendar.MINUTE));
System.out.println("Today date: " + today.get(Calendar.YEAR)+ "-" + today.get(Calendar.MONTH) + "-" + today.get(Calendar.DAY_OF_MONTH));
}
}
-
Calendar months are zero-indexed (e.g., January = 0, February = 1, etc.). The results you receive are correct. If you want to format the output of the date, use java.text.DateFormat and/or java.text.SimpleDateFormat.
Formatting a Date Using a Custom Format
Similar Threads
-
By Nizar in forum ASP.NET
Replies: 1
Last Post: 01-24-2003, 10:10 AM
-
By Richard Curzon in forum .NET
Replies: 2
Last Post: 08-01-2001, 11:18 AM
-
By Fabio Luis De Paoli in forum Java
Replies: 0
Last Post: 03-03-2001, 01:12 PM
-
By Zane Thomas in forum .NET
Replies: 4
Last Post: 11-27-2000, 11:12 AM
-
By Blair in forum VB Classic
Replies: 19
Last Post: 11-13-2000, 02:12 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks