简体   繁体   English

如何从 Java 字符串转换为 hh:mm:ss 格式的日期?

[英]How to convert from Java String to Date of format hh:mm:ss?

  1. From one of my web service method call returns Strings which are float values:从我的 Web 服务方法调用之一返回浮点值的字符串:

    Ex: 375.677, 61.47 etc.例如:375.677、61.47 等。

  2. These values are nothing but minutes 931 and 500 seconds.这些值只不过是 931 分钟和 500 秒。
  3. But I have to convert this String to Date format of HH:MM:SS which means like this: Ex: 06:20:04.但我必须将此字符串转换为 HH:MM:SS 的日期格式,这意味着:例如:06:20:04。

I have looked into stack exchange questions and tried a solution below.我研究了堆栈交换问题并尝试了下面的解决方案。 But that is returning the invalid values.但那是返回无效值。 Here is the code I tried:这是我试过的代码:

    float f = Float.parseFloat("375.677");

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.add(Calendar.HOUR_OF_DAY, (int) f);
    calendar.set(Calendar.MINUTE, (int) ((f * 60) % 60));
    calendar.set(Calendar.SECOND, (int) ((f * 3600) % 3600));

    SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
    System.out.println(dateFormat.format(calendar.getTime()));

Please let me know how to resolve this.请让我知道如何解决这个问题。

Thanks in advance, Saku提前致谢,佐久

Only JDK 1.5 is necessary.只需要 JDK 1.5。

    String input = "375.677";
    int mins = 0;
    int secs = 0;
    StringTokenizer st = new StringTokenizer(input, ".");
    if (st.hasMoreTokens()) mins = Integer.parseInt(st.nextToken());
    if (st.hasMoreTokens()) secs = Integer.parseInt(st.nextToken());
    if (st.hasMoreTokens()) throw new IllegalStateException("Second dot not allowed!");

    int seconds = mins*60+secs;
    int ss = seconds % 60;
    seconds = seconds / 60;
    int mm = seconds % 60;
    seconds = seconds / 60;
    int hh = seconds;

    System.out.println(String.format("%02d:%02d:%02d", hh, mm, ss));

I'm guessing you mean minutes before the point, and seconds after it, where the minutes go up to 931 and seconds go up to 500. If so, try我猜你的意思是点之前的几分钟,之后的几秒钟,分钟上升到 931,秒上升到 500。如果是这样,请尝试

float f = Float.parseFloat("375.677");
int m = ((int) f);
int s = ((int) (f * 1000)) % 1000;
// handle second overflow
m += s / 60;
s %= 60;
// handle minute overflow
int h = m / 60;
m %= 60;

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, h);
calendar.set(Calendar.MINUTE, m);
calendar.set(Calendar.SECOND, s);

SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
System.out.println(dateFormat.format(calendar.getTime()));

Convert everything to seconds:将所有内容转换为秒:

    String timeString = "234.34";
    String[] timeValuesAfterSplit = timeString.split("\\.");
    int minutesToSeconds = Integer.parseInt(timeValuesAfterSplit[0]) * 60;
    int totalSeconds = minutesToSeconds + Integer.parseInt(timeValuesAfterSplit[1]);

Now get the time in the given format using LocalTime as:现在使用 LocalTime 获取给定格式的时间:

LocalTime localTime = LocalTime.ofSecondOfDay(totalSeconds);

And then the format that you need could be obtained by calling toString() on localTime .然后可以通过在localTime上调用toString()来获得您需要的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JAVA中将日期从字符串“ yyyyddmm hh:mm:ss”转换为字符串“ DD / MM / YYYY” - Convert date from string “yyyyddmm hh:mm:ss” to String “DD/MM/YYYY” in JAVA 如何将 java.util.Date 转换为 soap 支持的日期格式“yyyy-MM-dd'T'HH:mm:ss”与区域 id - How to convert java.util.Date to soap suppported date format “yyyy-MM-dd'T'HH:mm:ss” with zone id 如何将毫秒转换为“hh:mm:ss”格式? - How to convert milliseconds to "hh:mm:ss" format? 无法从 HH:mm:ss.SSS'Z' 转换为 HH:MM 格式的 Java - Not able to convert from HH:mm:ss.SSS'Z' to HH:MM format Java 如何以这种格式“HH:mm:ss”转换秒 - How to convert the seconds in this format "HH:mm:ss" 如何将 Java 中的 DMY HH:MM:SS 字符串转换为 Julian Date? - How to convert a D M Y HH:MM:SS string in Java to Julian Date? Java:如何将字符串 (HH:MM:SS) 转换为持续时间? - Java: How to convert a string (HH:MM:SS) to a duration? 如何将“2012-03-04 00:00:00.0”转换为日期格式为“dd-mm-yyyy HH:mm:ss”使用Java - How to convert “2012-03-04 00:00:00.0” to Date with Format “dd-mm-yyyy HH:mm:ss” Using Java Java如何从日期获取HH:mm:ss - Java How to get just HH:mm:ss from Date 在Android和Swift中将日期字符串转换为“ yyyy / mm / dd hh:mm:ss”格式 - Convert a date String to “yyyy/mm/dd hh:mm:ss” format in Android & Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM