「Java」TimestampクラスとCalendar クラスで令和、平成の開始日を確認するサンプル

元号で使用文字コード説明
name=平成,abbr=H,since=600220800000;
name=令和,abbr=R,since=1556668800000
※sinceの値はエポックミリ秒
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.java.study;
import java.sql.Timestamp;
import java.util.Calendar;
public class LongTime {
protected static String dptn = "%02d年%02d月%02d日";
public static void main(String[] args) {
// 令和開始 エポックミリ秒
String strA = "1556668800000";
// 平成開始 エポックミリ秒
String strB = "600220800000";
Timestamp timestampA = new Timestamp(Long.parseLong(strA));
String resA = startYear(timestampA);
System.out.println("令和開始日: " + resA);
Timestamp timestampB = new Timestamp(Long.parseLong(strB));
String resB = startYear(timestampB);
System.out.println("平成開始日: " + resB);
}
public static String startYear(Timestamp tmp) {
Calendar cd = Calendar.getInstance();
cd.setTimeInMillis(tmp.getTime());
int year = cd.get(Calendar.YEAR);
int month = cd.get(Calendar.MONTH) + 1;
int day = cd.get(Calendar.DATE);
String startDay = String.format(dptn, year, month, day).toString();
return startDay;
}
}
package com.arkgame.java.study; import java.sql.Timestamp; import java.util.Calendar; public class LongTime { protected static String dptn = "%02d年%02d月%02d日"; public static void main(String[] args) { // 令和開始 エポックミリ秒 String strA = "1556668800000"; // 平成開始 エポックミリ秒 String strB = "600220800000"; Timestamp timestampA = new Timestamp(Long.parseLong(strA)); String resA = startYear(timestampA); System.out.println("令和開始日: " + resA); Timestamp timestampB = new Timestamp(Long.parseLong(strB)); String resB = startYear(timestampB); System.out.println("平成開始日: " + resB); } public static String startYear(Timestamp tmp) { Calendar cd = Calendar.getInstance(); cd.setTimeInMillis(tmp.getTime()); int year = cd.get(Calendar.YEAR); int month = cd.get(Calendar.MONTH) + 1; int day = cd.get(Calendar.DATE); String startDay = String.format(dptn, year, month, day).toString(); return startDay; } }
package com.arkgame.java.study;

import java.sql.Timestamp;
import java.util.Calendar;

public class LongTime {

      protected static String dptn = "%02d年%02d月%02d日";

      public static void main(String[] args) {

            // 令和開始 エポックミリ秒
            String strA = "1556668800000";
            // 平成開始 エポックミリ秒
            String strB = "600220800000";

            Timestamp timestampA = new Timestamp(Long.parseLong(strA));
            String resA = startYear(timestampA);
            System.out.println("令和開始日: " + resA);

            Timestamp timestampB = new Timestamp(Long.parseLong(strB));
            String resB = startYear(timestampB);
            System.out.println("平成開始日: " + resB);

      }

      public static String startYear(Timestamp tmp) {

            Calendar cd = Calendar.getInstance();
            cd.setTimeInMillis(tmp.getTime());
            int year = cd.get(Calendar.YEAR);
            int month = cd.get(Calendar.MONTH) + 1;
            int day = cd.get(Calendar.DATE);

            String startDay = String.format(dptn, year, month, day).toString();
            return startDay;

      }
}

実行結果
令和開始日: 2019年05月01日
平成開始日: 1989年01月08日

Java

Posted by arkgame