0

I want my String "10:10" to be converted into time in a format of hh:mm instead of MM/dd/yyyy hh:mm:ss aa

Date date;
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("hh:mm");
try 
{
    date = sdf.parse(a.getString("time").toString());
    Log.d(TAG, "onCreate: "+date);
    Log.d(TAG, "onCreate: "+a.getString("time"));
}
 catch (ParseException e) {
    e.printStackTrace();
}

But i am getting this in my logcat

D/MTAG: onCreate: Thu Jan 01 10:00:00 GMT+05:00 1970
D/MTAG: onCreate: 10:00
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Omar Saleem
  • 137
  • 1
  • 3
  • 11
  • 3
    Possible duplicate of [Parse time of format hh:mm:ss](https://stackoverflow.com/questions/11994790/parse-time-of-format-hhmmss) – vinS Dec 27 '17 at 03:19
  • 2
    I think you are wrongly understand Date and DateFormat. If you print out date object without any date formatter, it will display full date string. – anhtuannd Dec 27 '17 at 03:27
  • 1
    The `LocalTime` class of JSR-310 (the modern Java date and time API AKA `java.time`) is made for you! You may use JSR-310 on Android through ThreeTenABP, see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Dec 27 '17 at 05:30
  • 1
    An addition, `SimpleDateFormat` is long outdated and notoriously troublesome, and the modern date and time API is so much nicer to work with. I should say it’s a safe bet. – Ole V.V. Dec 27 '17 at 05:34
  • 1
    A `Date` cannot have a format in it, it always prints as `Thu Jan 01 10:00:00 GMT+05:00 1970` (its `toString` method produces this format). Any format is always outside the `Date` and in a `String`. See [All about java.util.Date](https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/). – Ole V.V. Dec 27 '17 at 05:38
  • Possible duplicate of [Java Date changing format](https://stackoverflow.com/questions/44306533/java-date-changing-format) – Ole V.V. Dec 27 '17 at 05:41

3 Answers3

2
    String timeString = "10:10";
    LocalTime time = LocalTime.parse(timeString);
    System.out.println(time);

This prints

10:10

I am cheating a bit, though. The way I read your question, you asked for a date-time object with the format HH:mm in it. Neither a LocalTime object (used in the above snippet) nor a Date (used in your code) can have a format in it. What you get when you concatenate the Date to a string or print the LocalTime is the result of the object’s toString method, and you cannot change this method (only in subclasses, and you don’t want that). In other words, when you want a specific format, you need to have that format in a string outside the date-time object.

The lucky part is that LocalTime.toString() produces the format you want (as long as the seconds and fraction of second are zero; otherwise they would be in the string too).

Will that work on your Android device? It will. LocalTime is a class of JSR-310 also known as java.time, the modern Java date and time API introduced nearly 4 years ago, early in 2014. JSR-310 has been backported to Java 6 and 7 in ThreeTen Backport, which in turn has been adapted to Android in ThreeTenABP. So get ThreeTenABP, add it to your project and start enjoying how much nicer it is to work with than the outdated date and time classes.

PS There’s a bug in your format pattern string in the question: Lowercase hh is for hour within AM or PM from 1 through 12, which works nicely when the hours are 10, but not always. I am convinced that you want uppercase HH for hour of day in the interval 0 through 23. When I run your code with a string of 12:12, I get Thu Jan 01 00:12:00 CET 1970. The hours are 0, not 12.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

Read Document

Try this:

  DateFormat aFormatter = new SimpleDateFormat("HH:mm");
  Date dt = aFormatter.parse("10:10");
  Calendar aCalander = Calendar.getInstance();
  aCalander.setTime(dt);
  int hour = aCalander.get(Calendar.HOUR);
  int minute = aCalander.get(Calendar.MINUTE);
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
1

When a Date is created, it will be in the format of "Thu Jan 01 10:00:00 GMT+05:00 1970" as you mention in your output. So if you want to display and play around with the format of the date you should use the format method in SimpleDateFormat. I hope the below code help you understand better.

//formatting date in Java using SimpleDateFormat
String date_s = "10:10"; 
SimpleDateFormat dt = new SimpleDateFormat("hh:mm"); 
Date date = dt.parse(date_s); 

System.out.println(date);   
System.out.println(dt.format(date));
Akjun
  • 351
  • 4
  • 13
  • 1
    FYI, this Answer uses troublesome old date-time classes now supplanted by the *java.time* classes. For earlier Android, see the *ThreeTen-Backport* & *ThreeTenABP* projects. Much better: `LocalTime.parse( "10:10" )` – Basil Bourque Dec 27 '17 at 06:20