-3

I need to convert a time stamp that currently is in string format "08.00" to a valid time in java so I later can compare time. How do I convert this string to time?

MTplus
  • 2,077
  • 4
  • 34
  • 51

5 Answers5

4

Something like this

        DateFormat sdf = new SimpleDateFormat("hh:mm");
        Date date = sdf.parse(time);            
  • 1
    @MTplus - the Date class is old now and largely deprecated, and I'm pretty sure if you print `date` here, it will assume 01/01/70, so be wary of that. – achAmháin Oct 22 '18 at 12:36
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Oct 23 '18 at 07:18
2

Instead of using the Date and/or SimpleDateFormat classes, perhaps consider LocalTime

String time = "08:00";
LocalTime lt = LocalTime.parse(time);
System.out.println(lt);

Output:

08:00

And can compare to other times easily with LocalTime::isBefore() or LocalTime::isAfter()

Example

achAmháin
  • 4,176
  • 4
  • 17
  • 40
1

Try below code,

String time = "08.00"; 
try {
    DateFormat sdfInput = new SimpleDateFormat("hh.mm");
    Date date = sdfInput.parse(time);

    DateFormat sdfOutput = new SimpleDateFormat("hh:mm");
    Log.e( "Time: ", sdfOutput.format(date));
} catch (Exception e) {
    e.printStackTrace();
}

Output -> Time: 08:00

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
0

The easiest way to do so is with a SimplDateFormatter:

DateFormat sdf = new SimpleDateFormat("hh:mm")

Then you can call Date date = sdf.parse(*your time string*)

Now you have your time as a valid Date object.

0

I have timeformat like this hhmmss="151918" so you can use any format instead of hhmmss according to your current time format like "hh.mm" or hh:mm:ss etc and you can call this method form any where you needed.

 fun convertTimeFormat(time:String):String{
        var formattedTime=""
        try {
            val inputFormat: DateFormat = SimpleDateFormat("hhmmss")
            val timeObj = inputFormat.parse(time)
            Log.d("timeObj",""+timeObj)
            formattedTime=SimpleDateFormat("hh:mm aa").format(timeObj)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return formattedTime
    }
Mudassir Khan
  • 1,714
  • 1
  • 20
  • 25
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Apr 14 '19 at 03:57
  • Also it doesn’t seem to answer the question, which was about getting objects that could be compared. Finally I think that your method gives a wrong result if the string was `121918`. You need to watch the case of the format pattern letters. There’s a difference between `hh` and `HH`. – Ole V.V. Apr 14 '19 at 03:59