简体   繁体   English

如何通过java分别将数据库字段中的时间转换为integer的小时、分钟、秒?

[英]how to convert the time from the database field into integer for hours, minutes, seconds separately through java?

i am using the DataType Time in my database it stores the data in the form as 01:23:14.0000000.在我的数据库中使用 DataType Time,它将数据以01:23:14.0000000 的形式存储。 i am using dateTime controls of sWt as我正在使用 sWt 的 dateTime 控件作为

DateTime dateTime = new DateTime(cmpGrid, SWT.BORDER | SWT.TIME);

i can save the Value of DateTime easily as我可以轻松地将 DateTime 的值保存为

String hours =String.valueOf(timeTo.getHours());
String minutes =String.valueOf(timeTo.getMinutes());
String seconds =String.valueOf(timeTo.getSeconds());
objhallModel.setClose_time(hours + ":"+minutes + ":" + seconds);

For Editting purpose i need to set The time value in my design view For that i want to use出于编辑目的,我需要在我的设计视图中设置时间值为此我想使用

dateTime.setTime(hours, minutes, seconds);

it takes all hours, minutes, second in integer... can please anybody tell me how to set The time in DateTime of Database time value.它需要 integer 中的所有小时、分钟、秒... 请任何人告诉我如何设置数据库时间值的 DateTime 中的时间。 thanks in advance提前致谢

I think you are using org.eclipse.swt.widgets.DateTime in your user interface and java.sql.Time to store in your database.我认为您在用户界面中使用org.eclipse.swt.widgets.DateTimejava.sql.Time存储在数据库中。

I note that the javadoc of Time says:我注意到时间的 javadoc 说:

The date components should be set to the "zero epoch" value of January 1, 1970 and should not be accessed.日期组件应设置为 1970 年 1 月 1 日的“零纪元”值,并且不应访问。

Therefore, I think you should set it as follows:因此,我认为您应该将其设置如下:

DateTime dateTime; // you have this already

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(0); // set to zero epoch
calendar.set(Calendar.HOUR, dateTime.getHours());
calendar.set(Calendar.MINUTE, dateTime.getMinutes());
calendar.set(Calendar.SECOND, dateTime.getSeconds());

Time time = new Time(calendar.getTimeInMillis());

The reverse operation (maybe this is what you want, I wasn't sure from your question) would be:反向操作(也许这是你想要的,我从你的问题中不确定)将是:

Time time; // you have this from your Database query
DateTime dateTime; // you have this already or will create a new one

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time.getTime());
dateTime.setTime(calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE),calendar.get(Calendar.SECOND));

First get the date from the database - Date date = resultset.getDate(int) or resultset.getDate(String) , then首先从数据库中获取日期 - Date date = resultset.getDate(int)resultset.getDate(String) ,然后

String hr = String.valorOf(date.getHours());
String min = String.valorOf(date.getMinutes());
String sec = String.valorOf(date.getSeconds());

Now you have the hour, minutes and the seconds.现在你有了小时、分钟和秒。

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

相关问题 如何将整数拆分为字符串并按秒/分钟/小时自动转换 - How to split an integer into String and Convert it automatically by seconds/minutes/hours Java代码将秒转换为分钟,小时和天 - Java Code to convert seconds into minutes, hours and days 在JAVA中将时间字段H:M转换为整数字段(分钟) - Convert time field H:M into integer field (minutes) in JAVA 如何使用JSpinner分别更改小时,分钟和秒? - How to change hours, minutes, and seconds separately using a JSpinner? Java 将秒转换为具有独特输出的天、小时、分钟、秒 - Java convert seconds to days, hours, minutes, seconds with unique outputs 如何将自纪元以来的秒数转换为Java中的小时/分钟/秒? - How can I convert seconds since the epoch to hours/minutes/seconds in Java? 将时间转换为秒,然后将其转换回小时(JAVA) - Convert time to seconds and then convert it back to hours (JAVA) 使用Java的转换器从数秒到数天,数小时,数分钟和数秒的问题 - Problems with a converter from seconds to day, hours, minutes, and seconds using Java 在 Java 中使用 Duration 对象失败将天、分钟、小时转换为秒 - Convert days, minutes, hours to seconds in Java Using Duration object failing Java类-将n小时,分钟和秒添加到一个时间 - Java class - add n hours, minutes, and seconds to a time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM