You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SimpleDateFormat用hh:mm:ss格式时12点时段显示异常问题咨询

Fixing SimpleDateFormat's 12:xx → 00:xx Display Issue

Hey there, I see you're facing a frustrating problem where times between 12:00-12:59 are showing up as 00:00-00:59 in your startDateText TextView when using SimpleDateFormat.format(), but 13:00 and later display correctly. This is almost always caused by mixing up 12-hour vs 24-hour format patterns, or quirks in the old date/time API's parsing logic.

The Better Solution: Use Java 8+'s java.time API

The legacy SimpleDateFormat and Date classes are thread-unsafe and prone to these kinds of confusing bugs. The modern java.time API (introduced in Java 8, fully supported on Android) is cleaner, safer, and fixes this issue out of the box. Here's your refactored code:

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import android.widget.TextView;

// Method to update your TextView with the correct time
public void setStartTime(String timeInput) {
    // Define a 24-hour format formatter
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
    
    try {
        // Parse the input string into a LocalTime object
        LocalTime parsedTime = LocalTime.parse(timeInput, timeFormatter);
        // Format it back and set to your TextView
        startDateText.setText(parsedTime.format(timeFormatter));
    } catch (Exception e) {
        // Handle invalid input gracefully
        e.printStackTrace();
        startDateText.setText("Invalid Time");
    }
}

// Test cases:
setStartTime("12:00"); // Will display "12:00"
setStartTime("13:00"); // Will display "13:00"

If You Must Stick with SimpleDateFormat

If you can't switch to the modern API yet, make sure you're using the 24-hour format pattern HH:mm for both parsing and formatting, and explicitly set a time zone to avoid unexpected shifts:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import android.widget.TextView;

public void setStartTime(String timeInput) {
    // Use HH:mm for 24-hour time, and set a specific time zone
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // Adjust to your local time zone
    
    try {
        Date parsedDate = sdf.parse(timeInput);
        startDateText.setText(sdf.format(parsedDate));
    } catch (Exception e) {
        e.printStackTrace();
        startDateText.setText("Invalid Time");
    }
}

Why This Happened

The core issue is likely that you were using the 12-hour format pattern hh instead of HH for 24-hour time. In SimpleDateFormat:

  • hh = 12-hour clock (1-12)
  • HH = 24-hour clock (0-23)

When parsing a 24-hour time like "12:00" with hh:mm, the API gets confused and interprets it as midnight (00:00) instead of noon. Using HH:mm ensures the parser and formatter both respect 24-hour time correctly.

内容的提问来源于stack exchange,提问作者sthor69

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:46:41