Why does the difference between 30 March and 1 March 2020 erroneously give 28 days instead of 29?

The problem is that because of Daylight Saving Time shift (on Sunday, March 8, 2020), there are 28 days and 23 hours between those dates. TimeUnit.DAYS.convert(…) truncates the result to 28 days. To see the problem (I’m in US Eastern time zone): SimpleDateFormat fmt = new SimpleDateFormat(“dd-MM-yyyy HH:mm:ss”); long diff = fmt.parse(“30-03-2020 00:00:00”).getTime() – fmt.parse(“1-03-2020 … Read more

Java: int array initializes with nonzero elements

Here we are faced with a bug in the JIT-compiler. Compiler determines that the allocated array is filled after allocation in Arrays.fill(…), but the check for uses between the allocation and the fill is faulty. So, compiler performs an illegal optimization – it skips zeroing of allocated array. This bug is placed in Oracle bug … Read more

How to set IntelliJ IDEA Project SDK

For a new project select the home directory of the jdk eg C:\Java\jdk1.7.0_99 or C:\Program Files\Java\jdk1.7.0_99 For an existing project. 1) You need to have a jdk installed on the system. for instance in C:\Java\jdk1.7.0_99 2) go to project structure under File menu ctrl+alt+shift+S 3) SDKs is located under Platform Settings. Select it. 4) click … Read more

How should I use try-with-resources with JDBC?

I realize this was long ago answered but want to suggest an additional approach that avoids the nested try-with-resources double block. public List<User> getUser(int userId) { try (Connection con = DriverManager.getConnection(myConnectionURL); PreparedStatement ps = createPreparedStatement(con, userId); ResultSet rs = ps.executeQuery()) { // process the resultset here, all resources will be cleaned up } catch (SQLException … Read more

How to set -source 1.7 in Android Studio and Gradle

Java 7 support was added at build tools 19. You can now use features like the diamond operator, multi-catch, try-with-resources, strings in switches, etc. Add the following to your build.gradle. android { compileSdkVersion 19 buildToolsVersion “19.0.0” defaultConfig { minSdkVersion 7 targetSdkVersion 19 } compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } } Gradle 1.7+, Android gradle … Read more

Java 7 language features with Android

If you are using Android Studio, the Java 7 language should be enabled automatically without any patches. Try-with-resource requires API Level 19+, and NIO 2.0 stuff are missing. If you can’t use Java 7 features, see @Nuno’s answer on how to edit your build.gradle. The following is for historical interest only. A small part of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)