Dot NET
Dot NET

Reputation: 4897

How to modify my code to only get the time portion of a date?

I've got the following code:

Date time = new SimpleDateFormat("HH:mm").parse("8:00");

When I call time.toString(), the following is produced:

Thu Jan 01 08:00:00 CET 1970

Is there any way I can extract just the 8:00 from it? I have searched far and wide and have not found any way to do it using the standard SimpleDateFormat.

Upvotes: 1

Views: 126

Answers (5)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79395

java.time

Existing answers are correct but use the legacy date-time API, which was the right thing to do in 2012.

In March 2014, the modern Date-Time API supplanted the legacy date-time API. Since then, it has been strongly recommended to switch to java.time, the modern date-time API.

The java.time API has LocalTime to represent just time.

String strTime = "8:00";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("H:mm", Locale.ENGLISH);
LocalTime time = LocalTime.parse(strTime, parser);
System.out.println(time);

// An example of formatting time to a custom format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);
String formatted = time.format(formatter);
System.out.println(formatted);

Output:

08:00
08:00:00

Online Demo

Learn about the modern date-time API from Trail: Date Time

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502086

When I call time.toString(), the following is produced

Yes, it would be - because you're calling Date.toString. A Date value has no concept of format.

Is there any way I can extract just the 8:00 from it?

Whenever you want to convert to a string, you should use a DateFormat. So use the same format that you parsed in.

Alternatively, use Joda-Time, which has a LocalTime type specifically for "time of day", and has a handy parse method. You should still use a formatter every time you want to convert to a string, but at least the value will be easier to work with and more descriptive before then.

LocalTime localTime = LocalTime.parse("8:00");

To format this, you can use something like ISODateTimeFormat.hourMinute() or if you might have more precision, perhaps ISODateTimeFormat.hourMinuteSecond() - see the docs for all of the many options available.

Upvotes: 3

recycle your original SimpleDateFormat Object

SimpleDateFormat format = new SimpleDateFormat("HH:mm")
Date time = format.parse("8:00");
String outString = format.format(time);

in case you were wondering, Here's some more information on DateTime Masks

Upvotes: 2

PermGenError
PermGenError

Reputation: 46428

java.util.Date class represents a specific instant in time, with millisecond precision.

API says java.util.Date.toString()

Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy

In order to format date's use SimpleDateFormat class

    System.out.println(new SimpleDateFormat("HH:mm").format(time));

Upvotes: 0

Oleg Šelajev
Oleg Šelajev

Reputation: 3790

Use the same SimpleDateFormat instance to format date into string.

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date time = sdf.parse("8:00");
System.out.println(sdf.format(time));

This will print:

08:00

Upvotes: 1

Related Questions