user3230289
user3230289

Reputation: 59

Convert integer to dates

I'm stuck at this part where I must use integer to get the individual year, month ,day, hour and min to string it into a date format, e.g. 24/01/2004 13:00.

Date Date = (years,month,day,hour,min);// error Part 
System.out.println(Date);

Upvotes: 3

Views: 760

Answers (5)

Ozan
Ozan

Reputation: 4415

The JodaTime library is very practical to create Dates just the way you were looking for:

// omission of DateTimeZone parameter results in use of JVM default time zone
DateTime dt = new DateTime( years, month, day, hour, min, DateTimeZone.forID( "Europe/Berlin" ) );
Date date = dt.toDate();

http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html

Upvotes: 1

Paul Hicks
Paul Hicks

Reputation: 14029

If you want to get a headstart on Java 8, you could use joda-time's LocalDateTime class, which shares quite a few method signatures with the Java 8 LocalDateTime. Though unfortunately it doesn't have LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute) or similar methods; instead, use the constructors.

Upvotes: 0

i-bob
i-bob

Reputation: 423

try this:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
.
.
.

DateFormat formatData = new SimpleDateFormat("d/M/yyyy H:mm");
System.out.println(formatData.format(cal.getTime()));

Upvotes: 0

nachokk
nachokk

Reputation: 14413

Use Calendar#set(..)

Calendar calendar = Calendar.getInstance();
calendar.set(year,month-1,day,hour,min);//month is 0 based
Date date = calendar.getTime();

Upvotes: 3

Vinayak Pingale
Vinayak Pingale

Reputation: 1315

Make use of link below to Set attributes of Date class.

Date releaseDate = new Date();
releaseDate.setYear(year);
releaseDate.setMonth(month);
......
releaseDate.setMinutes(min);

Refer Java docs Here

Upvotes: 0

Related Questions