bluelurker
bluelurker

Reputation: 1444

Creating a custom Date object from Calendar?

I need to create a custom Date object. I get a user input in 24 hr format like hh:mm i.e. 17:49 hrs. I want to set the hours and minutes as 17 and 49 respectively keeping other details like month , year , day as per the current time.For e.g if today is Dec 16,2014 and the time is 16:00 hrs , then i want a date object as Dec 16,2014 with time as 17:49 hrs. I know i can do this using deprecated apis , however i do not want to use them.I need the date object because i need to pass it to a java timer as java timer does not support any calendar object.

The user input comes as a string and i can parse that string using new SimpleDateFormat(HH:mm) contructor. I tried using the Calendar.set apis but had no success.

Could some one give some direction on how to proceed.

PS. Sorry , i can't use Joda time :)

Upvotes: 1

Views: 7591

Answers (2)

MihaiC
MihaiC

Reputation: 1583

EDIT

Since getHours() and getMinutes() are deprecated and have been replaced with Calendar.HOUR_OF_DAY and Calendar.MINUTE respectively, you could use an intermediary calendar object or set the YEAR and DAY to current values.


There is a problem though, if you input next day hour like 24:01 it won't move to the next day. The previous answer, with splitting of string did the overflowing correctly.


public static void main(String[] args) throws ParseException {
    final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
    final String timeInterval = "12:01";
    Date date = simpleDateFormat.parse(timeInterval);
    final Calendar calendar = Calendar.getInstance(); //calendar object with current date
    calendar.setTime(date); //set date with day january 1 1970, this is because you parsed only the time part, the date objects assumes you start from it's lowest value, try a System.out.println(date) before this to see.
    calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
    calendar.set(Calendar.DATE,Calendar.getInstance().get(Calendar.DATE));
    calendar.set(Calendar.MONTH, Calendar.getInstance().get(Calendar.MONTH))
    date = calendar.getTime();
    System.out.println(date);
}

You can use Calendar to set the time, then extract the date with .getTime() method, which returns a java.util.Date

The idea is to split your string into two parts based on the separator :.

Then simply initialize the calendar and set the hour and minutes with those values.

public static void main(String[] args) throws ParseException {
    final String userInput = "17:49";
    final String[] timeParts=userInput.split(":");
    Calendar cal=Calendar.getInstance(); //current moment calendar
    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeParts[0]));
    cal.set(Calendar.MINUTE, Integer.parseInt(timeParts[1]));
    cal.set(Calendar.SECOND,0); //if you don't care about seconds
    final Date myDate=cal.getTime(); //assign the date object you need from calendar
    //use myDate object anyway you want ...
    System.out.println(myDate);
}

Upvotes: 3

bluelurker
bluelurker

Reputation: 1444

The following code works fine , however i don't want to use any deprecated apis.

...
...
String timeInterval = "18:01";
Date date = simpleDateFormat.parse(timeInterval);
Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, date.getHours());
calendar.set(Calendar.MINUTE, date.getMinutes());
date = calendar.getTime();

System.out.println(date);
...
...

Upvotes: 0

Related Questions