使用 SimpleDateFormat 解析时间字符串

发布于 2024-12-07 06:09:02 字数 981 浏览 0 评论 0原文

我在解析数据库中的时间字符串时遇到问题

private static final String TIME_FORMAT = "HH:mm:ss";
public static final SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT, Locale.getDefault());

并遵循输出:

String time = "17:17:57";
Date myParsedDate = timeFormat.parse(time); //forget the exception thing
//output of myParsedDate.toString = "Thu Jan 01 16:47:57 GMT+08:00 1970"

似乎区域设置或其他问题存在问题..只是一个简单的字符串..为什么会这样?我只是希望日期是我的时间选择器所需的时间..哎呀..

编辑 因为我使用的是静态时间格式,所以我决定使用我的小助手方法,

public static Date getTimeFromTimeString(String timeString)
{
    String[] splitStrings = timeString.split(":");

    Date timeDate = new Date();
    timeDate.setHours(Integer.parseInt(splitStrings[0]));
    timeDate.setMinutes(Integer.parseInt(splitStrings[1]));
    timeDate.setSeconds(Integer.parseInt(splitStrings[2]));

    return timeDate;
}

感谢 Jon Skeet 的帮助。我相信这是时区偏移造成的。..我只是坚持我的小方法。

I have a problem parsing just the time string from my database

private static final String TIME_FORMAT = "HH:mm:ss";
public static final SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT, Locale.getDefault());

And following the output:

String time = "17:17:57";
Date myParsedDate = timeFormat.parse(time); //forget the exception thing
//output of myParsedDate.toString = "Thu Jan 01 16:47:57 GMT+08:00 1970"

It seems as though theres a problem with the locale or something.. just a simple string.. Why is this so? i just want the date to be the having the time i need for my time picker.. gee..

Edit
Because i am using a static time format i decided to use my little helper method

public static Date getTimeFromTimeString(String timeString)
{
    String[] splitStrings = timeString.split(":");

    Date timeDate = new Date();
    timeDate.setHours(Integer.parseInt(splitStrings[0]));
    timeDate.setMinutes(Integer.parseInt(splitStrings[1]));
    timeDate.setSeconds(Integer.parseInt(splitStrings[2]));

    return timeDate;
}

Thanks for the help Jon Skeet.. I believe it is the TimeZone offset that is causing this.. .. ill just stick to my little method..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱要勇敢去追 2024-12-14 06:09:02

问题是您正在打印 Date.toString() 的结果 - 它始终显示系统时区的结果。这可能不是您想要的。

Date 本身没有时区的概念 - 它只是时间的一个瞬间。

如果可能的话,我建议您使用 Joda Time。它有一个 LocalTime 它实际上代表了您在这里解析的内容。

编辑:只是为了重申评论中的内容...我怀疑您的 DateFormat 中的时区与 Date.toString 使用的时区不同。为了简单起见,可能值得将 DateFormat 的时区设置为 UTC,然后如果您想转换为 Calendar,您应该设置 在调用 setTime(date) 之前也将时区设置为 UTC。

The problem is you're printing out the result of Date.toString() - which always shows the results in the system time zone. That's probably not what you want.

The Date itself has no concept of a time zone - it's just an instant in time.

I would suggest you use Joda Time if at all possible. That has a LocalTime which actually represents what you're parsing here.

EDIT: Just to reiterate what's in the comment... I suspect that the time zone in your DateFormat is not the same as the time zone used by Date.toString. For simplicity, it's probably worth setting the DateFormat's time zone to UTC, and then if you want to convert to a Calendar you should set that time zone to UTC as well before calling setTime(date).

清醇 2024-12-14 06:09:02

只需这样做:

 Calendar c = Calendar.getInstance();
 c.setTime(myParsedDate);

 int hours = c.get(Calendar.HOUR);
 int mins = c.get(Calendar.MINUTE);
 int seconds = c.get(Calendar.SECOND);

Just do this:

 Calendar c = Calendar.getInstance();
 c.setTime(myParsedDate);

 int hours = c.get(Calendar.HOUR);
 int mins = c.get(Calendar.MINUTE);
 int seconds = c.get(Calendar.SECOND);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文