facebook twitter hatena line email

Java/日付

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

日付比較

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date date = sdf.parse("2012-10-10 10:10");
Date today = new Date();
if (today.compareTo(date) == 1) {
  // dateが昔
} else if (today.compareTo(date) == 0) {
  // dateが今日と同じ
} else if (today.compareTo(date) == -1) {
  // dateが未来
}

年月日曜日取得

Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DATE);
int week = cal.get(Calendar.DAY_OF_WEEK);
int h = cal.get(Calendar.HOUR_OF_DAY);//時を取得
int m = cal.get(Calendar.MINUTE);     //分を取得
int s = cal.get(Calendar.SECOND);      //秒を取得

hh:mm:ssから秒数取得

Calendarが推奨

try {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Date date = sdf.parse("01:02:03");
    cal.setTime(date);
    System.out.println("calendar hour=" + cal.get(Calendar.HOUR_OF_DAY));
    System.out.println("calendar minute=" + cal.get(Calendar.MINUTE));
    System.out.println("calendar second=" + cal.get(Calendar.SECOND));
    System.out.println("total sec=" + (cal.get(Calendar.HOUR_OF_DAY) * 3600 + cal.get(Calendar.MINUTE) * 60 + cal.get(Calendar.SECOND))); // 3723
    SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss.SSS");
    Date datems = sdf2.parse("01:02:03.123");
    cal.setTime(datems);
    System.out.println("calendar hour=" + cal.get(Calendar.HOUR_OF_DAY));
    System.out.println("calendar minute=" + cal.get(Calendar.MINUTE));
    System.out.println("calendar second=" + cal.get(Calendar.SECOND));
    System.out.println("total sec=" + (cal.get(Calendar.HOUR_OF_DAY) * 3600 + cal.get(Calendar.MINUTE) * 60 + cal.get(Calendar.SECOND))); // 3723
} catch(ParseException e) {
    e.printStackTrace();
}

参考:https://docs.oracle.com/javase/jp/7/api/java/util/Date.html

Dateは非推奨

try {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Date date = sdf.parse("01:02:03");
    System.out.println("second=" + date.getSeconds());
    System.out.println("min=" + date.getMinutes());
    System.out.println("hour=" + date.getHours());
    System.out.println("total sec=" + (date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds()));
    SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss.SSS");
    Date datems = sdf2.parse("01:02:03.123");
    System.out.println("second=" + datems.getSeconds());
    System.out.println("min=" + datems.getMinutes());
    System.out.println("hour=" + datems.getHours());
    System.out.println("total sec=" + (datems.getHours() * 3600 + datems.getMinutes() * 60 + datems.getSeconds()));
} catch(ParseException e) {
    e.printStackTrace();
}


SimpleDateFormat

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); // 2019-01-24T18:33:07.785+0900
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); // 2019-01-24T18:33:07.785+09
System.out.println(sdf.format(date));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String ymd = sdf.format(date);
ymd = ymd.substring(0, 26) + ":" + ymd.substring(26, 28); // 2019-01-24T18:33:07.785+09:00
日付/時刻パターン	結果
"yyyy.MM.dd G 'at' HH:mm:ss z"	2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy"	Wed, Jul 4, '01
"h:mm a"	12:08 PM
"hh 'o''clock' a, zzzz"	12 o'clock PM, Pacific Daylight Time
"K:mm a, z"	0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa"	02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z"	Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ"	010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"	2001-07-04T12:08:56.235-0700

参考:https://docs.oracle.com/javase/jp/6/api/java/text/SimpleDateFormat.html

unixtime

int unixtime = System.currentTimeMillis() / 1000L;
String unixtimeStr = String.valueOf(unixtime);

msからHH:MM:SS.mmmへ

public static String getHHMMSSmmByMs(int ms) {
       long hour = (ms / (1000 * 60 * 60)) % 24;
       long minute = (ms / (1000 * 60))  % 60;
       long second = (ms / 1000)  % 60;
       long millisSec = ms % 1000;
       return String.format("%02d:%02d:%02d.%03d", hour, minute, second, millisSec);
}

参考: https://qiita.com/rild/items/cef1229a74ba2a9298b3