Solved

create a date for the first of month

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

how ?
0
Comment
Question by:letsbedecent
7 Comments
 

Author Comment

by:letsbedecent
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 ?
0
 
LVL 92

Expert Comment

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

Expert Comment

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

Cheers,
Mukundh

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

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

Accepted Solution

by:
mukundha_expert earned 105 total points
ID: 16891794
Sorry ,

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1);
Date first = cal.getTime();
0
 
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).
0
 

Author Comment

by:letsbedecent
ID: 16891884
Thank you !
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

679 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