0
0

More than 1 year has passed since last update.

日時の文字列とUNIX時間の簡易的な取り扱い

Last updated at Posted at 2023-01-11

日時を表す文字列と、UNIX時間のlong値を扱うサンプル。
簡易的な取り扱いだけど、タイムゾーンを考慮。でもやっぱり簡易的なので、閏秒はまとめられ、サマータイムは扱えません。

サンプルコード
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Sample0115a
{
	/**
	 * 日時を表す文字列とUNIX時間(long)のサンプルコード
	 *
	 * 厳密ではないけれどタイムゾーンを意識した日時の数値とテキストの変換を行うサンプル。
	 *
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args)
		throws Exception
	{
		//対象となる年月日と時分秒とミリ秒
		final String DATE_TIME = "2023-01-12,07-04-48,975";
		
		//対象の地域
		final String DATE_TIME_ZONE = "Asia/Tokyo";
		
		//対象の形式
		final String DATE_TIME_PATTERN = "yyyy-MM-dd,HH-mm-ss,SSS";
		
		//処理の流れ
		//テキスト形式の日時が最初にあったとして
		//1)どの地域での日時だったか(タイムゾーン指定): String
		//2)日時の形式: String
		//の2つを使って、UNIX時間のミリ秒単位のカウント値を得る。これが基準の値になる。
		//
		//UNIX時間からテキスト形式に変換する際に、どの地域に向けて時間軸を変化させるか指定することになる。
		//同じUNIX時間の瞬間、地球上ではどこかでは朝、どこかでは昼、どこかでは夜。
		//
		//UNIX時間からテキストへの変換は
		//1)地域: String
		//2)形式: String
		//の2つを使ってテキストに変換することになる。
		//
		//この方式は閏秒をなんとなく扱う。なので完全な時間管理ではない。そしてサマータイムも考慮していない。
		//実際はわずかに長かったり短かったりするわけだけど、きっかりした数値(UNIX時間)なので
		//数値だけみてなんとなくわかった感じになれる。手持ちが何もない状態ではそれだけでありがたい。
		
		long time = 0L;
		String s1 = null;
		
		System.out.print("original: ");
		System.out.println(DATE_TIME);
		
		//日時のテキストから、地域と形式の2つを使ってUNIX時間を得る
		time = sample1(DATE_TIME, DATE_TIME_ZONE, DATE_TIME_PATTERN);
		System.out.print("time: ");
		System.out.println(time);
		
		//UNIX時間から、地域と形式の2つを使って日時テキストを得る
		//元と同じ地域を指定した場合、元と同じ日時テキストになるはず。
		s1 = sample2(time, DATE_TIME_ZONE, DATE_TIME_PATTERN);
		System.out.print("[Asia/Tokyo]: ");
		System.out.println(s1);
		
		//同じUNIX時間の瞬間、パリでは何時何分だったのか
		s1 = sample2(time, "Europe/Paris", DATE_TIME_PATTERN);
		System.out.print("[Europe/Paris]: ");
		System.out.println(s1);
		
		return;
	}
	
	
	static long sample1(final String date_time,final String date_time_zone,final String date_time_pattern)
		throws ParseException
	{
		long result = 0L;
		final SimpleDateFormat format = new SimpleDateFormat(date_time_pattern);
		final TimeZone zone = TimeZone.getTimeZone(date_time_zone);
		Date d1 = null;
		
		format.setTimeZone(zone);
		d1 = format.parse(date_time);
		result = d1.getTime();
		
		return result;
	}
	
	
	static String sample2(final long date_time,final String date_time_zone,final String date_time_pattern)
	{
		String result = null;
		final SimpleDateFormat format = new SimpleDateFormat(date_time_pattern);
		final TimeZone zone = TimeZone.getTimeZone(date_time_zone);
		final Date d1 = new Date(date_time);
		
		format.setTimeZone(zone);
		result = format.format(d1);
		
		return result;
	}
}
結果
original: 2023-01-12,07-04-48,975
time: 1673474688975
[Asia/Tokyo]: 2023-01-12,07-04-48,975
[Europe/Paris]: 2023-01-11,23-04-48,975
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0