Solved

create a date for the first of month

Posted on 2006-06-12
7
268 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 55
Arrays.asList  VS  ArrayList 4 60
even odd program using while loop 3 41
Android development question 2 37
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

810 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