草庐IT

this_date

全部标签

java - 为什么Java静态方法可以调用构造函数,但不能引用this?

我的假设:静态方法不能调用非静态方法。构造函数是一种没有返回类型的方法。给定这个例子...publicclassMain{publicstaticvoidmain(String[]args){Mainp=newMain();//constructorcallk();//[implicit]`this`reference}protectedMain(){System.out.print("1234");}protectedvoidk(){}}此行打印1234:Mainp=newMain()这一行抛出异常:k()为什么示例代码会做这两件事?它们不会与我的上述假设冲突吗?我的假设是否正确?

Java日历问题: why are these two Dates not equal?

importjava.io.*;publicclasstesting{publicstaticvoidmain(Stringa[])throwsException{Dated1=newDate();Thread.sleep(2000);Dated2=newDate();if(d1.equals(d2)){System.out.println("Bothequal");}else{System.out.println("Bothnotequal");}Calendarc1=Calendar.getInstance();Calendarc2=Calendar.getInstance();c

java - 字符串 -> java.util.Date -> java.sql.Date(带时间戳)

这是我的问题:我有一个用户输入一个日期,如:2012-12-24(字符串)我将时间连接到该字符串,然后转换为java.util.Date我的代码如下所示:Stringtempstartdate=startdte;//startdteisthestringvaluefromatxtfieldtempstartdate+="00:01:00";Stringtempenddate=startdte;tempenddate+="23:59:59";SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");java.util.Dat

java - 在 setter 中使用 "this"

我看不出以下两种创建setter的方法之间有什么真正的区别,但我想知道我是不是太天真了。一个比另一个更受欢迎吗?publicvoidfooSetter(Stringbar){_bar=bar;}publicvoidfooSetter(Stringbar){this._bar=bar;} 最佳答案 在这种情况下没有语义差异,因为没有歧义。另一方面,如果您的实例字段也称为bar,则需要使用this来消除歧义:publicvoidfooSetter(Stringbar){this.bar=bar;}

javax.naming.NameNotFoundException : Name [jdbc/rhwebDB] is not bound in this Context. 无法找到 [jdbc]

我知道有很多关于此异常的问题,但是,我相信我已经尝试了很多天的所有方法,但还没有成功。由于这是一个生产服务器,我只能在午夜后处理它:(我有一个Tomcat应用程序。最近,我更新了连接池,以便使用Tomcat的jdbc-connectionpool。在我的Windows开发机器中,一切正常,但现在我正尝试在我的Linux服务器上实现它,每当我的应用程序尝试连接到MySQL时,我都会收到此异常(见标题)。我正在使用“EasyTomcat7”,它应该与普通版本的Tomcat相同,只是它带有CPanel软件。我只需要此数据库可用于此应用程序(而不是多个应用程序)。这是我的Java数据库类:pu

java - JPA 2.1 : Introducing Java 8 Date/Time API

我想在启用JPA的应用程序中添加对Java8日期/时间API(JSR-310)的支持。很明显JPA2.1doesnotsupporttheJava8Date/TimeAPI.作为解决方法,最常见的建议是使用AttributeConverter.在我现有的应用程序中,我将我的实体更改为对列映射字段使用LocalDate/LocalDateTime类型,并为java.util添加了旧版setter/getter.Date给他们。我创建了相应的AttributeConverter类。当使用Query.setParameter()和java.util.Date实例时,我的应用程序现在失败了(它

java - 如何将 util.Date 转换为 time.LocalDate 以正确处理 1893 年之前的日期

我google了一下,最常用的方法好像是date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();但是,对于1893-04-01之前的日期,此方法似乎会失败以下测试在我的机器上失败,结果为1893-03-31而不是1893-04-01:@TestpublicvoidtestBeforeApril1893()throwsParseException{Datedate=newSimpleDateFormat("yyyy-MM-dd").parse("1893-04-01");System.out.println(date);

java.time : DateTimeParseException for date "20150901023302166"

这个问题在这里已经有了答案:Isjava.timefailingtoparsefraction-of-second?(3个答案)关闭7年前。LocalDateTime.parse("20150901023302166",DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"))给出错误:java.time.format.DateTimeParseException:Text'20150901023302166'couldnotbeparsedatindex0

java - "synchronized(this)"与 Java 中的 "synchronized((BaseClass)this)"?

这是我之前问题的后续问题,Isthisvariablebeingsafelyaccessedbyusingsynchronization?对于下面的程序,ClassSubClassBextendsSuperClassA{protectedintc;publicvoidinc(){synchronized(this){c++;}}publicvoiddec(){synchronized((SuperClassA)this){c--;}}}计数器“c”会被线程安全地访问吗?我不确定在“dec()”方法中,SuperClassA强制转换“this”是否引用了同步块(synchronizedb

java swing 与 mvc : is this pattern indeed possible?

我是swing的新手,但设法创建了一个下降的gui。但我的问题是我无法应用引用/教程中建议的模式,主要是MVC模式。是我的问题,还是在JTree中使用SwingWorker,无法将controller/view/model明确分离?例如,我使用Swingworker,但我无法在设计中“安装”Controller。即控件的Action本质上是在内部的doBackground方法中,例如按钮的Action执行。所以没有Controller类。swingworker中的操作结果是更新一个Jtree,所以我将结果传递给一个我命名为model的类,但是这个类必须能够访问JFrame内部Jtree