「Java/日付」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→Dateは非推奨) |
(→SimpleDateFormat) |
||
行77: | 行77: | ||
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700 | "EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700 | ||
"yyMMddHHmmssZ" 010704120856-0700 | "yyMMddHHmmssZ" 010704120856-0700 | ||
− | + | "yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700 | |
</pre> | </pre> | ||
2019年1月24日 (木) 18:29時点における版
日付比較
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm"); Date ddate = sdf.parse("2012-10-10 10:10"); Date today = new Date(); if (today.compareTo(ddate) == 1) { // ddateが昔 } else if (today.compareTo(ddate) == 0) { // ddateが今日と同じ } else if (today.compareTo(ddate) == -1) { // ddateが未来 }
年月日曜日取得
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(now.HOUR_OF_DAY);//時を取得 int m = cal.get(now.MINUTE); //分を取得 int s = cal.get(now.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
例
日付/時刻パターン 結果 "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);