简体   繁体   English

需要 Java 中的 SWT DateTime 帮助

[英]Require help for SWT DateTime in Java

I retrieve a Date from the database using the ResultSet rsExpid .我使用ResultSet rsExpid从数据库中检索Date

Date joindate = rsExpid.getDate("join_date_ad");
System.out.println(joindate);
int year = joindate.getYear();
System.out.println(year);
int month =joindate.getMonth();
System.out.println(month);
int day = joindate.getDay();
System.out.println(day);
dateTimeJoin4.setDate(year, month, day);

When I print joindate to the console it shows correctly as 2011-08-03 , but when I print the year to the console I was amazed to see 111 .当我将joindate打印到控制台时,它正确显示为2011-08-03 ,但是当我将year打印到控制台时,我很惊讶地看到111 Similarly printing the month produced 7 and the day resulted in 3 .同样打印月份产生7和日期产生3

The variable dateTimeJoin4 is my SWT DateTime .变量dateTimeJoin4是我的 SWT DateTime It is not setting any value and it is also not giving any error message.它没有设置任何值,也没有给出任何错误消息。 Could please anybody help me on this?可以请任何人帮助我吗?

Chances are you haven't read the documentation for Date.getYear and Date.getMonth :您可能还没有阅读Date.getYearDate.getMonth的文档:

Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.返回一个值,该值是从包含或以此日期 object 表示的时间点开始的年份减去 1900 的结果,如本地时区所解释。

and

Returns a number representing the month that contains or begins with the instant in time represented by this Date object.返回一个数字,该数字表示包含此日期 object 所表示的时间点或以该时间点开始的月份。 The value returned is between 0 and 11, with the value 0 representing January.返回的值介于 0 和 11 之间,其中值 0 表示一月。

respectively.分别。 Likewise Date.getDay returns the day of the week, not the day of the month - you want Date.getDate() for that (or preferrably a different API entirely, either Calendar or Joda Time).同样Date.getDay返回星期几,而不是月份中的哪一天 - 你想要Date.getDate() (或者最好是完全不同的 API, Calendar或 Joda 时间)。

This has nothing to do with SWT's DateTime type - after all, you only use that in the last line.这与 SWT 的DateTime类型无关——毕竟,您只在最后一行使用它。 When something behaves unusually, your first port of call should be the documentation.当某事表现异常时,您的第一个停靠点应该是文档。 SWT's DateTime.setDate method is documented to require a year between 1752 and 9999, so 111 will be confusing it. SWT 的DateTime.setDate方法被记录为需要 1752 到 9999 之间的一年,所以 111 会混淆它。 Admittedly it would have been nice if it had thrown an exception, but even so...诚然,如果它抛出异常会很好,但即便如此......

The fact that you're calling deprecated methods should have been a hint to you, although Calendar also uses 0-11 for its months.您正在调用已弃用的方法这一事实应该对您有所提示,尽管Calendar在其月份中使用 0-11。

Personally I would strongly encourage you to use Joda Time for as much of your date and time work as you can.就我个人而言,我强烈建议您尽可能多地使用Joda Time进行约会和时间工作。 It's a far superior date and time API to the one built into Java.这是一个远远优于 Java 中内置的日期和时间 API 的日期和时间。 It's not immediately clear whether it's worth you using it here (if this is all you have to do) but if you're doing anything at all significant (including parsing, formatting or any kind of arithmetic) you should be using it.目前尚不清楚是否值得在这里使用它(如果这就是你所要做的),但如果你正在做任何重要的事情(包括解析、格式化或任何类型的算术),你应该使用它。

I tried using this method and it worked correctly so this method can be useful to other.我尝试使用此方法并且它工作正常,因此此方法对其他人有用。

Calendar startCal = Calendar.getInstance();
startCal.setTime(joindate);
int months= startCal.get(Calendar.MONTH);
int years= startCal.get(Calendar.YEAR);
int days= startCal.get(Calendar.DAY_OF_MONTH);
System.out.println(months+1);
System.out.println(years);
System.out.println(days);
dateTimeJoin4.setDate(years, months+1, days);

getDay() return the position of the day not the date like suppose today is Monday it will return 1, it start from the sunday with 0 value. getDay() 返回当天的 position,而不是假设今天是星期一的日期,它将返回 1,它从星期日开始,值为 0。

as well as the getMonth() return the position as starting value is 0 for january at now you getting the 7 instead of 8.以及 getMonth() 返回 position 因为一月的起始值为 0,现在您得到的是 7 而不是 8。

for getting date you can use the SimpleDateFormat or DateFormat by passing the format string in that you can get it要获取日期,您可以通过传递格式字符串来使用 SimpleDateFormat 或 DateFormat,您可以获得它

example links示例链接

http://www.roseindia.net/java/javadate/date-format.shtml http://www.roseindia.net/java/javadate/date-format.shtml

http://www.java-examples.com/java-simpledateformat-class-example http://www.java-examples.com/java-simpledateformat-class-example

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM