Link to home
Start Free TrialLog in
Avatar of letsbedecent
letsbedecent

asked on

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 ?
Avatar of letsbedecent
letsbedecent

ASKER

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 ?
Avatar of Mick Barry
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 1);
Date first = cal.getTime();
it should be
cal.set ( Calendar.MONTH , 0 );  for January.

Cheers,
Mukundh

you can also set by cal.set ( Calendar.MONTH, Calendar.JANUARY )
ASKER CERTIFIED SOLUTION
Avatar of mukundha_expert
mukundha_expert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you !