I have a date
in Integer format
(YYYYMMDD). And a start_time
as a String
(HH:mm 24 hour system). and a time_duration
in hours as a double
.
int date = 20140214;
String start_time = "14:30";
double duration = 50.30;
I want to use these 3 values and create 2 Java Date Objects. One is start_date
and one is end_date
. They should be in the format YYYY-MM-DD HH:mm
.
And then after I get 2 data Strings like YYYY-MM-DD HH:mm
. how can I obtain those previous variables. date
, start_time
, duration
.
This is my attempt.
public void solve() throws IOException {
int date = 20140214;
String start_time = "14:30";
double duration = 24.50;
String startDate = "";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
startDate = getDate(date) + " " + start_time;
try {
Date start_Date = df.parse(startDate);
Date end_Date = new Date(start_Date.getTime()+(int)(duration*3600*1000));
System.out.println(df.format(start_Date));
System.out.println(df.format(end_Date));
} catch (ParseException ex) {
}
}
public String getDate(int dateInt) {
String date = "";
String dateIntString = String.valueOf(dateInt);
date = date + dateIntString.substring(0, 4) + "-";
date = date + dateIntString.substring(4, 6) + "-";
date = date + dateIntString.substring(6, 8);
return date;
}
Is there any easy way to do it. ? Or some built-in capabilities I can use other than those I have used ?