• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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