如何在Android中将结束时间获取为浮点数。

huangapple go评论109阅读模式
英文:

How to get end time as float in Android

问题

  1. I had a String time format like 08:25 now how can I convert to float time value
  2. String guestclosetime = getCurrentTime();
  3. SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
  4. timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
  5. try {
  6. System.out.println("starttime" + TestRideFragment.getCurrentTime());
  7. // convert12to24format(guestclosetime);
  8. Date date1 = timeFormat.parse("00:20");//run
  9. Date date2 = timeFormat.parse(guestclosetime);
  10. //Date date3=timeFormat.parse(idle_time);
  11. long sum = date1.getTime() + date2.getTime();
  12. String date3 = timeFormat.format(new Date(sum));
  13. System.out.println("The sum is " + date3);// 08:28
  14. now I need the date3 as float how can I do in android.
  15. 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
  16. I need like 1.30 = 1.5 like that
英文:

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

  1. String guestclosetime = getCurrentTime();
  2. SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
  3. timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
  4. try {
  5. System.out.println("starttime"+ TestRideFragment.getCurrentTime());
  6. // convert12to24format(guestclosetime);
  7. Date date1 = timeFormat.parse("00:20");//run
  8. Date date2 = timeFormat.parse(guestclosetime);
  9. //Date date3=timeFormat.parse(idle_time);
  10. long sum = date1.getTime() + date2.getTime();
  11. String date3 = timeFormat.format(new Date(sum));
  12. 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

答案1

得分: 1

  1. ## java.time 和 ThreeTenABP
  2. 我建议您使用 `java.time`,这是现代的 Java 日期和时间 API,来进行时间计算。您不能使用 `Date` 来表示一段时间。而是可以使用 `LocalTime` 表示一天中的某个时间,使用 `Duration` 表示持续时间,即一段时间的长度。
  3. ```java
  4. LocalTime dropOff = LocalTime.of(8, 30);
  5. System.out.println("Drop-off: " + dropOff);
  6. Duration timeToDest = Duration.ofMinutes(20);
  7. LocalTime end = dropOff.plus(timeToDest);
  8. System.out.println("Time at destination: " + end);

输出:

  1. > Drop-off: 08:30
  2. > Time at destination: 08:50

要转换为表示自午夜以来的小时数的 float

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

问题:java.time 不是需要 Android API 等级 26 吗?

java.time 在旧版本和新版本的 Android 设备上都可以很好地工作。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及较新的 Android 设备(从 API 等级 26 开始),这个现代 API 已经内置。
  • 在非 Android 环境中,Java 6 和 7 可以使用 ThreeTen Backport,这是现代类的后移版本(ThreeTen for JSR 310)。在底部的链接中可以找到更多信息。
  • 在(较旧的) Android 设备上,请使用 ThreeTenABP 的 Android 版本。它被称为 ThreeTenABP。并且确保从 org.threeten.bp 及其子包中导入日期和时间类。

链接

  1. <details>
  2. <summary>英文:</summary>
  3. ## java.time and ThreeTenABP
  4. 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.
  5. LocalTime dropOff = LocalTime.of(8, 30);
  6. System.out.println(&quot;Drop-off: &quot; + dropOff);
  7. Duration timeToDest = Duration.ofMinutes(20);
  8. LocalTime end = dropOff.plus(timeToDest);
  9. System.out.println(&quot;Time at destination: &quot; + end);
  10. Output:
  11. &gt; Drop-off: 08:30
  12. &gt; Time at destination: 08:50
  13. To convert to a `flaot` indicating the number of hours since midnight:
  14. float hoursSinceMidnight = (float) end.getLong(ChronoField.NANO_OF_DAY)
  15. / (float) Duration.ofHours(1).toNanos();
  16. System.out.println(&quot;As float: &quot; + hoursSinceMidnight);
  17. &gt; As float: 8.833334
  18. ## Question: Doesn’t java.time require Android API level 26?
  19. java.time works nicely on both older and newer Android devices. It just requires at least **Java 6**.
  20. - In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  21. - 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).
  22. - 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.
  23. ## Links
  24. - [Oracle tutorial: Date Time](https://docs.oracle.com/javase/tutorial/datetime/) explaining how to use java.time.
  25. - [Java Specification Request (JSR) 310](https://jcp.org/en/jsr/detail?id=310), where `java.time` was first described.
  26. - [ThreeTen Backport project](http://www.threeten.org/threetenbp/), the backport of `java.time` to Java 6 and 7 (ThreeTen for JSR-310).
  27. - [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP), Android edition of ThreeTen Backport
  28. - [Question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project), with a very thorough explanation.
  29. </details>
  30. # 答案2
  31. **得分**: 0
  32. 这里
  33. String date3 = timeFormat.format(new Date(sum));
  34. // 首先,您必须将&quot;:&quot;替换为&quot;.&quot;,以获取可以转换为浮点数的有效字符串
  35. String strDate = date3.replace(&quot;:&quot;, &quot;.&quot;);
  36. // 使用Float.valueOf()进行转换
  37. Float floatValue = Float.valueOf(strDate);
  38. <details>
  39. <summary>英文:</summary>
  40. Here
  41. String date3 = timeFormat.format(new Date(sum));
  42. // First you have to replace &quot;:&quot; to &quot;.&quot; to get valid string that can be converted to float
  43. String strDate = date3.replace(&quot;:&quot;, &quot;.&quot;);
  44. // Use Float.value() to convert
  45. Float floatValue = Float.valueOf(strDate);
  46. </details>
  47. # 答案3
  48. **得分**: 0
  49. ```java
  50. public static float HoursToFloat(String tmpHours) {
  51. float result = 0;
  52. tmpHours = tmpHours.trim();
  53. try {
  54. result = new Float(tmpHours);
  55. } catch (NumberFormatException nfe) {
  56. if (tmpHours.contains(":")) {
  57. int hours = 0;
  58. float minutes = 0;
  59. try {
  60. hours = Integer.parseInt(tmpHours.split(":")[0]);
  61. minutes = Integer.parseInt(tmpHours.split(":")[1]);
  62. } catch (Exception nfe2) {
  63. throw nfe2;
  64. }
  65. if (minutes > 0) {
  66. result = minutes / 60;
  67. }
  68. result += hours;
  69. }
  70. }
  71. return result;
  72. }
英文:

Is that your goal?

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

If you try it right

  1. public static float HoursToFloat(String tmpHours) {
  2. float result = 0;
  3. tmpHours = tmpHours.trim();
  4. try {
  5. result = new Float(tmpHours);
  6. } catch (NumberFormatException nfe) {
  7. if (tmpHours.contains(&quot;:&quot;)) {
  8. int hours = 0;
  9. float minutes = 0;
  10. try {
  11. hours = Integer.parseInt(tmpHours.split(&quot;:&quot;)[0]);
  12. minutes = Integer.parseInt(tmpHours.split(&quot;:&quot;)[1]);
  13. } catch (Exception nfe2) {
  14. throw nfe2;
  15. }
  16. if (minutes &gt; 0) {
  17. result = minutes / 60;
  18. }
  19. result += hours;
  20. }
  21. }
  22. return result;
  23. }

huangapple
  • 本文由 发表于 2020年5月30日 10:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/62097188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定