create a date for the first of month

Posted on 2006-06-12
Last Modified: 2010-03-31
i want to create a java.util.Date object for the first of the current month !

how ?
Question by:letsbedecent

Author Comment

ID: 16889299
I used

int month = Calendar.getInstance().get(Calendar.MONTH);
int year = Calendar.getInstance().get(Calendar.YEAR);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = sdf.parse(month + "/01/" + year);

is there a better way of doing this ?
LVL 92

Expert Comment

ID: 16889896
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 1);
Date first = cal.getTime();
LVL 10

Expert Comment

ID: 16891199
it should be
cal.set ( Calendar.MONTH , 0 );  for January.


Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 10

Expert Comment

ID: 16891283
you can also set by cal.set ( Calendar.MONTH, Calendar.JANUARY )
LVL 10

Accepted Solution

mukundha_expert earned 105 total points
ID: 16891794
Sorry ,

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1);
Date first = cal.getTime();
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 20 total points
ID: 16891875
>>  want to create a java.util.Date object for the first of the current month

Since it is current month, you don't need to set the month using cal.set ( Calendar.MONTH.... ). Just set the Calendar.DATE part as mukundha has shown (and yes, DATE starts with 1 whereas MONTH starts with 0).

Author Comment

ID: 16891884
Thank you !

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
Unhandled exception type Exception 18 42
Crystal Reports Licensing Questions 4 34
running on tomcat not jboss eap 7.0 3 17
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

791 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