开发者

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. i am using dateTime controls of sWt as

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

i can save the Value of DateTime easily as

String hours =Strin开发者_运维百科g.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. 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.

I note that the javadoc of Time says:

The date components should be set to the "zero epoch" value of January 1, 1970 and should not be accessed.

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜