user1446228
user1446228

Reputation: 17

Converting String to time only?

I'm getting a string from the database and converting it into a time, eg 12:19, and then I'm adding it to the timepicker. The problem is it's taking the time and date and I cant seem to get rid of the date part. My code is below

 public static final String TIME_FORMAT = "kk:mm";
    String StartTime = reminder.getString(reminder.getColumnIndexOrThrow(DatabaseStore.START_TIME));

 SimpleDateFormat dateTimeFormat = new SimpleDateFormat(TIME_FORMAT);
    startTime = dateTimeFormat.parse(StartTime); 

    addTimeTopicker.setTime(startTime); 

Upvotes: 0

Views: 2676

Answers (2)

Simon Dorociak
Simon Dorociak

Reputation: 33495

and coveting it into time eg 12:19 and then im adding it to the timepicker

You have bad pattern, you can't use kk.

So you can use these approaches:

public static final String TIME_FORMAT = "h:mm";
Result: 12:01 [h is hour in AM / PM (1-12)]

public static final String TIME_FORMAT = "k:mm";
Result: 12:01 [k is hour in day (1-24)]

Have look at SimpleDateFormat patterns

Upvotes: 1

Cruceo
Cruceo

Reputation: 6824

You're not telling the date formatter to use your specific format:

Date d = new Date(timeString);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String newtimeString = sdf.format(d);

That would return, for example: 12:19 PM

Upvotes: 0

Related Questions