Skip to content

Instantly share code, notes, and snippets.

@KingC100
Created January 23, 2014 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KingC100/8578247 to your computer and use it in GitHub Desktop.
Save KingC100/8578247 to your computer and use it in GitHub Desktop.
ビッグで閉まる
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
/**
* 入力されたBigDecimal型の数値が
* 正しい暦日のyyyy/MM/ddであるかチェックします。
*
* @author kiichi
*/
public class Cnv {
public static void main(String[] args) {
// BigDecimal型の変数を宣言
BigDecimal big = new BigDecimal(19931019);
// 型変換を行う関数を実行
boolean res = Convert(big);
// 入力された結果を画面出力
System.out.println(res);
}
/**
* BigDecimal型をyyyyMMdd形式へ変換、暦日チェックを行う関数
* @param param_big
* @return boolean
*/
protected static boolean Convert(BigDecimal param_big){
// BigDecimal型の変数をString型へ型変換
String str_ymd = param_big.toString();
// 入力値を画面出力
System.out.println(str_ymd);
// String型のyyyy(yyyyMMddの0番目~3番目の文字列)
String str_y = str_ymd.substring(0,4);
// String型のMM(yyyyMMddの4番目~6番目の文字列)
String str_m = str_ymd.substring(4,6);
// String型のdd(yyyyMMddの6番目~8番目の文字列)
String str_d = str_ymd.substring(6,8);
// String型のyyyy/MM/ddを生成
String date = str_y + "/" + str_m + "/" + str_d;
// DateFormatオブジェクト生成
DateFormat format = DateFormat.getDateInstance();
// 日時を厳密に解析するよう設定
format.setLenient(false);
try{
// 日時を解析
format.parse(date);
// 成功時はtrueを返り値とする
return true;
} catch (ParseException e){
// format.parse(Date)実行時にエラーとなった場合の処理
// エラー時はfalseを返り値とする
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment