-1

I had a String time format like 08:25 now how can I convert to float time value

    String guestclosetime = getCurrentTime();
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

    timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    try {

        System.out.println("starttime"+ TestRideFragment.getCurrentTime());
        // convert12to24format(guestclosetime);
        Date date1 = timeFormat.parse("00:20");//run
        Date date2 = timeFormat.parse(guestclosetime);
        //Date date3=timeFormat.parse(idle_time);

        long sum = date1.getTime() + date2.getTime();

        String date3 = timeFormat.format(new Date(sum));
        System.out.println("The sum is "+date3);// 08:28

now I need the date3 as float how can I do in android.

see I had a scenario as a driver drops a person at x place here I get the current time like 08:30 AM. and after dropping he needs to reach his destination and he took 20 mins this is in float need to convert as a string and if we add this 2 we need to get the total time as 08:50 now this total time need to save as float

I need like 1.30 = 1.5 like that

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Lahari Areti
  • 627
  • 2
  • 10
  • 30
  • Show us example output that you want. – elbert rivas May 30 '20 at 02:50
  • date3 string when convert to float like dis 20.12 – Lahari Areti May 30 '20 at 02:54
  • 1
    ?? 20.12 ? how ? – lantian May 30 '20 at 02:55
  • just i gave example like that value. here date3 I am getting like 08:28 this need to convert to float – Lahari Areti May 30 '20 at 02:56
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. May 31 '20 at 08:49
  • What sense does this make in your user’s domain? Is `00:20` a point in time (20 past midnight) or an amount of time (20 minutes)? Is `guestclosetime` a point in time or an amount? Your are definitely misusing `Date` when using it for both. You may also be misusing `float` for the result, I don’t understand what you’re doing well enough to tell. – Ole V.V. May 31 '20 at 08:51
  • What is the relation between 08:28 and 20.12? `if (date3.equals("08:28") yourFloat = 20.12f;` is easy, but what do you want in case of other values? – Ole V.V. May 31 '20 at 08:53

3 Answers3

1

java.time and ThreeTenABP

I recommend that you use java.time, the modern Java date and time API, for your time math. You cannot use a Date for an amount of time. Instead use LocalTime for a time of day and Duration for a duration, an amount of time.

    LocalTime dropOff = LocalTime.of(8, 30);
    System.out.println("Drop-off: " + dropOff);

    Duration timeToDest = Duration.ofMinutes(20);
    LocalTime end = dropOff.plus(timeToDest);
    System.out.println("Time at destination: " + end);

Output:

Drop-off: 08:30
Time at destination: 08:50

To convert to a flaot indicating the number of hours since midnight:

    float hoursSinceMidnight = (float) end.getLong(ChronoField.NANO_OF_DAY)
            / (float) Duration.ofHours(1).toNanos();
    System.out.println("As float: " + hoursSinceMidnight);
As float: 8.833334

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I think I guessed what you meant. I have changed my way of converting to `float` so it now gives what I think you were after. – Ole V.V. May 31 '20 at 11:29
0

Here

String date3 = timeFormat.format(new Date(sum));
// First you have to replace ":" to "." to get valid string that can be converted to float
String strDate = date3.replace(":", ".");
// Use Float.value() to convert
Float floatValue = Float.valueOf(strDate);
elbert rivas
  • 1,464
  • 1
  • 17
  • 15
  • see it should convert the date3 to related float time – Lahari Areti May 30 '20 at 03:00
  • see I had a scenario as a driver drops a person at x place here I get the current time like 08:30 AM. and after dropping he needs to reach his destination and he took 20 mins this is in float need to convert as a string and if we add this 2 we need to get the total time as 08:50 now this total time need to save as float – Lahari Areti May 30 '20 at 03:11
0

Is that your goal?

"1:30" => 1.5 "1.83" => 1.83 "0.5" => 0.5

If you try it right

public static float HoursToFloat(String tmpHours)  {
    float result = 0;
    tmpHours = tmpHours.trim();
    try {
        result = new Float(tmpHours);
    } catch (NumberFormatException nfe) {

        if (tmpHours.contains(":")) {
            int hours = 0;
            float minutes = 0; 
            try {
                hours = Integer.parseInt(tmpHours.split(":")[0]);
                minutes = Integer.parseInt(tmpHours.split(":")[1]); 
            } catch (Exception nfe2) {
                throw nfe2;
            }

            if (minutes > 0) {
                result = minutes / 60;
            }
            result += hours;
        }
    }
    return result;
}
Javad Dehban
  • 1,282
  • 3
  • 11
  • 24