使用 SimpleDateFormat 解析时间字符串
我在解析数据库中的时间字符串时遇到问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您正在打印
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 byDate.toString
. For simplicity, it's probably worth setting theDateFormat
's time zone to UTC, and then if you want to convert to aCalendar
you should set that time zone to UTC as well before callingsetTime(date)
.只需这样做:
Just do this: