JavaでLocalDateTimeのフォーマット変換処理

  • 2020.05.05
  • 2021.01.24
  • Java
NO IMAGE

今回はLocalDateTimeの変換処理をやっていきたいと思います。

前回にも便利な変換クラスの紹介をしているので、そちらもぜひご覧ください。

Javaで文字列や数値へのフォーマット変換の処理


今回は5個のパターンを書いてみました。
・LocalDateTimeからString(yyyy-MM-dd HH:mm:ss)に変換
・LocalDateTimeからString(yyyy)に変換
・LocalDateTimeからInteger(yyyyMMdd)に変換
・String(yyyy-MM-dd HH:mm:ss)からLocalDateTimeに変換
・String(yyyyMMddHHmmssSSS)からLocalDateTimeに変換

package common.util;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * LocalDateTimeフォーマット変換に関する処理を行うUtilクラス
 */
public class LocalDateTimeFormatUtil {
	
	/** 日付フォーマット 年月日 */
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	/** 日付フォーマット 年月日+時間 */
	public static final String DATE_FORMAT_YYYYMMDDHHMMSS = DATE_FORMAT + " HH:mm:ss";
	
	/** 日付フォーマット(yyyy-MM-dd) */
	public static SimpleDateFormat formatDate = new SimpleDateFormat(DATE_FORMAT);
	
	/** 日時フォーマット(yyyy-MM-dd) */
	public static DateTimeFormatter formatDateF = DateTimeFormatter.ofPattern(DATE_FORMAT);
	
	/** 日時フォーマット(yyyy-MM-dd HH:mm:ss) */
	public static DateTimeFormatter formatDateTimeHH = DateTimeFormatter.ofPattern(DATE_FORMAT_YYYYMMDDHHMMSS);
	
	/**
	 * LocalDateTimeからString(yyyy-MM-dd HH:mm:ss)に変換
	 * @return String型の日時(変換対象がnullの場合、null)
	 */
	public static String getFormatLocalDateTimeToString(LocalDateTime dateTime) {
		
		String str = null;
		if (dateTime != null) {
			str = dateTime.format(formatDateTimeHH);
		}
		return str;
	}
	
	/**
	 * LocalDateTimeからString(yyyy)に変換
	 * @return String型の日付(変換対象がnullの場合、null)
	 */
	public static String getFormatLocalDateToStringYear(LocalDateTime dateTime) {
		
		String str = null;
		if (dateTime != null) {
			str = dateTime.format(formatDateTimeHH);
			String[] arrayStr = str.split("-", 0);
			str = arrayStr[0];
		}
		return str;
	}
	
	/**
	 * LocalDateTimeからInteger(yyyyMMdd)に変換
	 * @return Integer型の日時(変換対象がnullの場合、null)
	 */
	public static Integer getFormatLocalDateTimeToInt(LocalDateTime dateTime) {

		Integer intDate = null;
		
		if (dateTime != null) {
			String yyyyMMdd = dateTime.format(formatDateF);
			
			String[] date = yyyyMMdd.split("-", 0);
			if (date.length == 3) {
				StringBuilder tmpDate = new StringBuilder();
				tmpDate.append(date[0]).append(date[1]).append(date[2]);
				intDate = Integer.parseInt(tmpDate.toString());
			}
		}
		return intDate;
	}
	
	/**
	 * String(yyyy-MM-dd HH:mm:ss)からLocalDateTimeに変換
	 * @return LocalDateTime型の日付(変換対象がnullの場合、null)
	 */
	public static LocalDateTime getFormatStringToLocalDateTime(String str) {
		
		LocalDateTime dateTime = null;
		if (str != null) {
			dateTime = LocalDateTime.parse(str, formatDateTimeHH);
		}
		return dateTime;
	}
	
	/**
	 * String(yyyyMMddHHmmssSSS)からLocalDateTimeに変換
	 * @return LocalDateTime型の日付(変換対象がnullの場合、null)
	 */
	public static LocalDateTime getFormatStringSSSToLocalDateTime(String str) {
		
		LocalDateTime dateTime = null;
		if (str != null && str.length() == 17) {
			dateTime = LocalDateTime.of(
				Integer.parseInt(str.substring(0, 4)),    // 年
				Integer.parseInt(str.substring(4, 6)),    // 月
				Integer.parseInt(str.substring(6, 8)),    // 日
				Integer.parseInt(str.substring(8, 10)),   // 時
				Integer.parseInt(str.substring(10, 12)),  // 分
				Integer.parseInt(str.substring(12, 14)),  // 秒
				Integer.parseInt(str.substring(14, 17).concat("000000"))); // ナノ
		}
		return dateTime;
	}
}

日付のフォーマットパターンを「/」にしたい場合や、
「-」がいらないパターンなどもあると思います。

13行目あたりの「final」が付いている日付フォーマットの部分を
「yyyy/MM/dd」にしたり「yyyyMMdd」に変更してあげると、
そのようになると思いますので、ぜひお試しあれ。

*今回は分かりやすくLocalDateTimeFormatUtilクラスと分けましたが、
FormatUtilクラス(前回の記事)にまとめて書いてあっても良いと思います。
そこはお好みで。

以上、LocalDateTimeに関する変換処理でした。

Javaカテゴリの最新記事