• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

create a date for the first of month

i want to create a java.util.Date object for the first of the current month !

how ?
0
letsbedecent
Asked:
letsbedecent
2 Solutions
 
letsbedecentAuthor Commented:
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
 
objectsCommented:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 1);
Date first = cal.getTime();
0
 
mukundha_expertCommented:
it should be
cal.set ( Calendar.MONTH , 0 );  for January.

Cheers,
Mukundh

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
mukundha_expertCommented:
you can also set by cal.set ( Calendar.MONTH, Calendar.JANUARY )
0
 
mukundha_expertCommented:
Sorry ,

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1);
Date first = cal.getTime();
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>>  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
 
letsbedecentAuthor Commented:
Thank you !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now