Javaの、基本的なところで見逃してしまった不具合を、自戒を込めてメモ…
キャストの順序により、補数によって負の数値として扱われていたのか…
long で宣言されていた time を計算で使っているので、括弧内の計算も long で計算されるとなんか脳内変換していた…テストする人として、見逃してはいけないような初歩的なミスだ…
※DateUtils使えば、とかの話は置いておいて…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.lang.Long; | |
| import java.lang.System; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Calendar; | |
| import java.util.Date; | |
| class Sample { | |
| public static void main(String[] args){ | |
| run(); | |
| } | |
| public static void run() { | |
| long time = new Date().getTime(); | |
| System.out.println(time); | |
| System.out.println(1000 * 60 * 60 * 24 * 30); | |
| // Example1 | |
| long millTime1 = time; | |
| System.out.println(millTime1); // 1445048760331 | |
| printFormatedData(millTime1); // 2015/10/17 11:26:00 | |
| // Example2 | |
| long millTime2 = time + (1000 * 60 * 60 * 24 * 30); | |
| System.out.println(millTime2); // 1443345793035 | |
| printFormatedData(millTime2); // 2015/09/27 18:23:13 | |
| // Example3 | |
| long millTime3 = time + (1000L * 60 * 60 * 24 * 30); | |
| System.out.println(millTime3); // 1447640760331 | |
| printFormatedData(millTime3); // 2015/11/16 11:26:00 | |
| // 10011010011111101100100000000000 | |
| // 1111111111111111111111111111111 | |
| // 10000000000000000000000000000000 | |
| System.out.println(Integer.toBinaryString(1000 * 60 * 60 * 24 * 30)); | |
| System.out.println(Integer.toBinaryString(Integer.MAX_VALUE)); | |
| System.out.println(Integer.toBinaryString(Integer.MIN_VALUE)); | |
| // 10011010011111101100100000000000 | |
| // 111111111111111111111111111111111111111111111111111111111111111 | |
| // 1000000000000000000000000000000000000000000000000000000000000000 | |
| System.out.println(Long.toBinaryString(1000L * 60 * 60 * 24 * 30)); | |
| System.out.println(Long.toBinaryString(Long.MAX_VALUE)); | |
| System.out.println(Long.toBinaryString(Long.MIN_VALUE)); | |
| } | |
| private static void printFormatedData(long time) { | |
| SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); | |
| System.out.println(simpleDateFormat.format(new Date(time))); | |
| } | |
| } |