Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to process a list of Strings which may or may not be times. When I do receive a time, it will need to be converted from "HH:mm:ss" to number of milliseconds before processing:

final String unknownString = getPossibleTime();    

final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
dateFormat.setLenient(false);
try {
    final Date date = dateFormat.parse(unknownString);
    //date.getTime() is NOT what I want here, since date is set to Jan 1 1970

    final Calendar time = GregorianCalendar.getInstance();
    time.setTime(date);

    final Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
    calendar.set(Calendar.SECOND, time.get(Calendar.SECOND));

    final long millis = calendar.getTimeInMillis();
    processString(String.valueOf(millis));
}
catch (ParseException e) {
    processString(unknownString);
}

This code works, but I really dislike it. The exception handling is particularly ugly. Is there a better way to accomplish this without using a library like Joda-Time?

share|improve this question
3  
Hmm, wouldn't this be more suited for codereview? – fge 21 hours ago
Why do you need the two calendars? – biziclop 21 hours ago
maybe avoiding using the calendar? docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime() – fGo 21 hours ago
on date there is a method getTime(); this should return your final long millis if I got it right – user1121883 21 hours ago
@fGo How can I make sure the date is set to today and not Jan 1 1970 with only one Calendar? Its possible with the deprecated Date getters (getHours(), getMinutes(), getSeconds()), but I would prefer not using deprecated methods. – dbyrne 21 hours ago

2 Answers

up vote 1 down vote accepted
public static long getTimeInMilliseconds(String unknownString) throws ParseException {

   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
   String dateString = dateFormat.format(Calendar.getInstance().getTime());

   DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   return timeFormat.parse(dateString + " " + unknownString).getTime();
}

Handle the ParseException outside of this method however you'd like. I.e. ("No time information provided"... or "unknown time format"... etc.)

.getTime() returns the time in milliseconds. It's part of the java.util.Date API.

share|improve this answer
The problem is that date is set to Jan 1 1970, not the current day. I'll update the code above with a comment to make that more clear. – dbyrne 21 hours ago
@dbryne Good catch - I tried something a bit different. Check again. – John Strickler 21 hours ago
SimpleDateFormat is not threadsafe so using static instances carries risk. – McDowell 15 hours ago
@McDowell Thanks, I didn't know that but it makes sense. – John Strickler 1 min ago

Why don't you first check if the input is actually of HH:mm:ss format. You can do this by trying match input to regex [0-9]?[0-9]:[0-9]?[0-9]:[0-9]?[0-9] first and if it matches then treat it as date otherwise call processString(unknownString);

share|improve this answer
(\d|[01]\d|2[0-3]):([0-5]\d):([0-5]\d) so you don't allow 30:73:99 as a valid time. – Eric Jablow 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.