ともさんのHP >プロブラミング >自作JavaLibrary >時刻を文字列に変換するクラス

時刻を文字列に変換するクラス

tomojavalib.util.Jikoku
時刻を文字に直す、文字を時刻に直すためのクラスです。
プログラムの実行時刻を記録したりする時によく使っています。
Javaでデジタルカメラの画像整理プログラムを作って自分用に使っていますが、 このクラスを使って画像のファイル名を撮影時刻に変更しています。
時刻の書式によっていくつかメソッドを用意しています。


stringNow*()は現在の時刻を文字列に直すクラスで、 書式により分けています。
stringNow() → "yyyymmddhhmmss"
stringNow2() → "yyyy年mm月dd日hh時mm分ss秒"
stringNow3() → "yyyy/mm/dd hh:mm:ss"


calendarToString*( Calendar c )はCalendar( Java 楽天 内で時間を扱うクラス)を文字列に直すメソッドです。
これも書式によっていくつか用意しています。
ちなみに、CalendarToString4( Calendar c )はグーグルに対応したサイトマップファイル作成プログラムを 作成する際に追加したもの。CalendarToString5( Calendar c )は同じくRSS.xmlファイル用です。
いろいろ書式がありすぎて困ります。
calendarToString1( Calendar c ) → "yyyymmddhhmmss"
calendarToString3( Calendar c ) → "yyyy/mm/dd mm:hh:ss"
CalendarToString4( Calendar c ) → "yyyy-mm-ddThh:mm:ss+09:00"
CalendarToString5( Calendar c ) → "Tue, 31 Aug 2004 19:03:22 +0900"


stringToCalendar*( String day )は逆に文字列をCalendarクラスに変換するメソッドです。
stringToCalendar1( String day ) → "yyyymmddhhmmss"
stringToCalendar3( String day ) → "yyyy/mm/dd hh:mm:ss"
stringToCalendar4( String day ) → "yyyy-mm-dd"


このクラスを使って、下記のように実行してみると...
    tomojavalib.util.Jikoku j = new tomojavalib.util.Jikoku();
    System.out.println( "現在の時刻 " + j.stringNow() );
    System.out.println( "現在の時刻 " + j.stringNow2() );
    System.out.println( "現在の時刻 " + j.stringNow3() );
    //文字をcalenderにして文字に戻す
    String moji = "20090522110601";
    Calendar c = j.stringToCalendar1( moji );
    System.out.println( "文字の変換 " + moji + " → " + j.calendarToString1( c ));
    System.out.println( "文字の変換 " + moji + " → " + j.calendarToString3( c ));   
    System.out.println( "文字の変換 " + moji + " → " + j.CalendarToString4( c ));
    System.out.println( "文字の変換 " + moji + " → " + j.CalendarToString5( c ));   
   
    //文字をcalenderにして文字に戻す
    moji = "2009/05/22 11:10:09";
    c = j.stringToCalendar3( moji );
    System.out.println( "文字の変換 " + moji + " → " + j.calendarToString1( c ));
    moji = "2009-05-22";
    c = j.stringToCalendar4( moji );
    System.out.println( "文字の変換 " + moji + " → " + j.calendarToString1( c ));   


実行結果

現在の時刻 20090603072909
現在の時刻 2009年06月03日07時29分09秒
現在の時刻 2009/06/03 07:29:09
文字の変換 20090522110601 → 20090522110601
文字の変換 20090522110601 → 2009/05/22 11:06:01
文字の変換 20090522110601 → 2009-05-22T11:06:01+09:00
文字の変換 20090522110601 → Fri, 22 May 2009 11:06:01+0900
文字の変換 2009/05/22 11:10:09 → 20090522111009
文字の変換 2009-05-22 → 20090522000000



以下はこのクラスのソース


package tomojavalib.util;


import java.util.*;


/**
* 時間に関するメソッドを収録
* 20090522util.classより再構成
* @author to.totomo.net
*/
public class Jikoku {

   
   /**
   * アプリケーション:main()
   * 機能:このclassの動作確認用
   */
   public static void main(String args[])
   {
    tomojavalib.util.Jikoku j = new tomojavalib.util.Jikoku();
    System.out.println( "現在の時刻 " + j.stringNow() );
    System.out.println( "現在の時刻 " + j.stringNow2() );
    System.out.println( "現在の時刻 " + j.stringNow3() );
    //文字をcalenderにして文字に戻す
    String moji = "20090522110601";
    Calendar c = j.stringToCalendar1( moji );
    System.out.println( "文字の変換 " + moji + " → " + j.calendarToString1( c ));
    System.out.println( "文字の変換 " + moji + " → " + j.calendarToString3( c ));   
    System.out.println( "文字の変換 " + moji + " → " + j.CalendarToString4( c ));
    System.out.println( "文字の変換 " + moji + " → " + j.CalendarToString5( c ));   
   
    //文字をcalenderにして文字に戻す
    moji = "2009/05/22 11:10:09";
    c = j.stringToCalendar3( moji );
    System.out.println( "文字の変換 " + moji + " → " + j.calendarToString1( c ));
    moji = "2009-05-22";
    c = j.stringToCalendar4( moji );
    System.out.println( "文字の変換 " + moji + " → " + j.calendarToString1( c ));   
   
   }



   /**
   * Clenderで入力した時刻を文字列に直す"yyyymmddhhmmss"形式
   */
   public String calendarToString1( Calendar c )
   {
    String stmp = "";
    int yr = c.get(Calendar.YEAR); int mn = c.get(Calendar.MONTH);
    int dy = c.get(Calendar.DATE); int hr = c.get(Calendar.HOUR_OF_DAY);
    int min = c.get(Calendar.MINUTE); int sec= c.get(Calendar.SECOND);
    String nowstring = Integer.toString(yr);
    stmp = Integer.toString( mn+1 ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    stmp = Integer.toString( dy ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    stmp = Integer.toString( hr ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    stmp = Integer.toString( min ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    stmp = Integer.toString( sec ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    c= null;
    return nowstring;
   }

   
   /**
   * Clenderで入力した時刻を文字列に直す"yyyy/mm/dd mm:hh:ss"形式
   */
   public String calendarToString3( Calendar c )
   {
    String stmp = "";
    int yr = c.get(Calendar.YEAR); int mn = c.get(Calendar.MONTH);
    int dy = c.get(Calendar.DATE); int hr = c.get(Calendar.HOUR_OF_DAY);
    int min = c.get(Calendar.MINUTE); int sec= c.get(Calendar.SECOND);
    String nowstring = Integer.toString(yr);
    stmp = Integer.toString( mn+1 ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + "/" + stmp;
    stmp = Integer.toString( dy ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + "/" + stmp;
    stmp = Integer.toString( hr ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + " " + stmp;
    stmp = Integer.toString( min ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + ":" + stmp;
    stmp = Integer.toString( sec ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + ":" + stmp;
    c= null;
    return nowstring;
   }

   /**
   * Clenderで入力した時刻を文字列に直す"yyyy-mm-ddThh:mm:ss+09:00"形式
   * 2007/5/12googleサイトマップ向けに作成
   */
   public String CalendarToString4( Calendar c )
   {
    String stmp = "";
    int yr = c.get(Calendar.YEAR); int mn = c.get(Calendar.MONTH);
    int dy = c.get(Calendar.DATE); int hr = c.get(Calendar.HOUR_OF_DAY);
    int min = c.get(Calendar.MINUTE); int sec= c.get(Calendar.SECOND);
    String nowstring = Integer.toString(yr);
    stmp = Integer.toString( mn+1 ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + "-" + stmp;
    stmp = Integer.toString( dy ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + "-" + stmp;
    stmp = Integer.toString( hr ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + "T" + stmp;
    stmp = Integer.toString( min ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + ":" + stmp;
    stmp = Integer.toString( sec ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + ":" + stmp + "+09:00";
    c= null;
    return nowstring;
   }


   /**
   * Clenderで入力した時刻を文字列に直す"Tue, 31 Aug 2004 19:03:22 +0900"形式
   * 2007/5/21作成 RSS.xml用
   */
   public String CalendarToString5( Calendar c )
   {
    String stmp = "";
    int yr = c.get(Calendar.YEAR); int mn = c.get(Calendar.MONTH);
    int dy = c.get(Calendar.DATE); int hr = c.get(Calendar.HOUR_OF_DAY);
    int min = c.get(Calendar.MINUTE); int sec= c.get(Calendar.SECOND);
    String nowstring = "";
    if( c.get(c.DAY_OF_WEEK) == c.MONDAY){ nowstring = "Mon, " ;}
    if( c.get(c.DAY_OF_WEEK) == c.TUESDAY){ nowstring = "Tue, " ;}
    if( c.get(c.DAY_OF_WEEK) == c.WEDNESDAY ){ nowstring = "Wed, " ;}
    if( c.get(c.DAY_OF_WEEK) == c.THURSDAY){ nowstring = "Thu, " ;}
    if( c.get(c.DAY_OF_WEEK) == c.FRIDAY){ nowstring = "Fri, " ;}
    if( c.get(c.DAY_OF_WEEK) == c.SATURDAY){ nowstring = "Sat, " ;}
    if( c.get(c.DAY_OF_WEEK) == c.SUNDAY){ nowstring = "Sun, " ;}
    stmp = Integer.toString( dy ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp +" ";
    if( (mn+1)==1){ stmp = "Jan" ;}
    if( (mn+1)==2){ stmp = "Feb" ;}
    if( (mn+1)==3){ stmp = "Mar" ;}
    if( (mn+1)==4){ stmp = "Apr" ;}
    if( (mn+1)==5){ stmp = "May" ;}
    if( (mn+1)==6){ stmp = "Jun" ;}
    if( (mn+1)==7){ stmp = "Jul" ;}
    if( (mn+1)==8){ stmp = "Aug" ;}
    if( (mn+1)==9){ stmp = "Sep" ;}
    if( (mn+1)==10){ stmp = "Oct" ;}
    if( (mn+1)==11){ stmp = "Nov" ;}
    if( (mn+1)==12){ stmp = "Dec" ;}
   nowstring = nowstring + stmp + " ";
   nowstring = nowstring + Integer.toString(yr);
    stmp = Integer.toString( hr ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + " " + stmp;
    stmp = Integer.toString( min ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + ":" + stmp;
    stmp = Integer.toString( sec ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + ":" + stmp + "+0900";
    c= null;
    return nowstring;
   }


   /**
    * 時刻を示す文字列をcalenderに変換します
   * 変換する文字列"yyyy-mm-dd"形式
   * @return 文字列を変換したCalendar
    2007/5/21 HP作成用に作成
   */
   public Calendar stringToCalendar4( String day )
   {
    Calendar c = Calendar.getInstance();   //一時使用カレンダの作成
    int year = Integer.parseInt( day.substring( 0,4 ) );
    int month = Integer.parseInt( day.substring( 5,7 ) ) - 1 ;
    int date = Integer.parseInt( day.substring( 8,10 ) );
    int hour = 0;
    int minute =0;
    int second =0;
   try{
    hour = Integer.parseInt( day.substring( 11,13 ) );
   }catch(Exception e){hour = 0; }
   try{
    minute = Integer.parseInt( day.substring( 14,16 ) );
   }catch(Exception e){minute =0; }
   try{
    second = Integer.parseInt( day.substring( 17,19 ) );
   }catch(Exception e){second =0; }
    c.set(year, month, date, hour, minute, second);
    return c;
   }

   /**
    * 時刻を示す文字列をcalenderに変換します
   * 変換する文字列"yyyy/mm/dd hh:mm:ss"形式
   * @return 文字列を変換したCalendar
   */
   public Calendar stringToCalendar3( String day )
   {
    Calendar c = Calendar.getInstance();   //一時使用カレンダの作成
    int year = Integer.parseInt( day.substring( 0,4 ) );
    int month = Integer.parseInt( day.substring( 5,7 ) ) - 1 ;
    int date = Integer.parseInt( day.substring( 8,10 ) );
    int hour = Integer.parseInt( day.substring( 11,13 ) );
    int minute = Integer.parseInt( day.substring( 14,16 ) );
    int second = Integer.parseInt( day.substring( 17,19 ) );
    c.set(year, month, date, hour, minute, second);
    return c;
   }
   
   
   /**
    * 時刻を示す文字列をcalenderに変換します
   * 変換する文字列"yyyymmddhhmmss"形式
   * @return 文字列を変換したCalendar
   */
   public Calendar stringToCalendar1( String day )
   {
    Calendar c = Calendar.getInstance();   //一時使用カレンダの作成
    int year = Integer.parseInt( day.substring( 0,4 ) );
    int month = Integer.parseInt( day.substring( 4,6 ) ) - 1 ;
    int date = Integer.parseInt( day.substring( 6,8 ) );
    int hour = Integer.parseInt( day.substring( 8,10 ) );
    int minute = Integer.parseInt( day.substring( 10,12 ) );
    int second = Integer.parseInt( day.substring( 12,14 ) );
    c.set(year, month, date, hour, minute, second);
    return c;
   }



   /**
   * 現在の時刻を文字列に直す"yyyy/mm/dd hh:mm:ss"形式
   * @return 現在時刻の文字列
   */
   public String stringNow3()
   {
    String stmp = "";
    Calendar c = Calendar.getInstance();   //一時使用カレンダの作成
    int yr = c.get(Calendar.YEAR); int mn = c.get(Calendar.MONTH);
    int dy = c.get(Calendar.DATE); int hr = c.get(Calendar.HOUR_OF_DAY);
    int min = c.get(Calendar.MINUTE); int sec= c.get(Calendar.SECOND);
    String nowstring = Integer.toString(yr) + "/";
    stmp = Integer.toString( mn+1 ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + "/";
    stmp = Integer.toString( dy ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + " ";
    stmp = Integer.toString( hr ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + ":";
    stmp = Integer.toString( min ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + ":";
    stmp = Integer.toString( sec ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + "";
    c= null;
    return nowstring;
   }


   /**
   * 現在の時刻を文字列に直す"yyyy年mm月dd日hh時mm分ss秒"形式
   * @return 現在時刻の文字列
   */
   public String stringNow2()
   {
    String stmp = "";
    Calendar c = Calendar.getInstance();   //一時使用カレンダの作成
    int yr = c.get(Calendar.YEAR); int mn = c.get(Calendar.MONTH);
    int dy = c.get(Calendar.DATE); int hr = c.get(Calendar.HOUR_OF_DAY);
    int min = c.get(Calendar.MINUTE); int sec= c.get(Calendar.SECOND);
    String nowstring = Integer.toString(yr) + "年";
    stmp = Integer.toString( mn+1 ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + "月";
    stmp = Integer.toString( dy ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + "日";
    stmp = Integer.toString( hr ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + "時";
    stmp = Integer.toString( min ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + "分";
    stmp = Integer.toString( sec ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp + "秒";
    c= null;
    return nowstring;
   }


   /**
   * 現在の時刻を文字列に直す"yyyymmddhhmmss"形式
   * @return 現在時刻の文字列
   */
   public String stringNow()
   {
    String stmp = "";
    Calendar c = Calendar.getInstance();   //一時使用カレンダの作成
    int yr = c.get(Calendar.YEAR); int mn = c.get(Calendar.MONTH);
    int dy = c.get(Calendar.DATE); int hr = c.get(Calendar.HOUR_OF_DAY);
    int min = c.get(Calendar.MINUTE); int sec= c.get(Calendar.SECOND);
    String nowstring = Integer.toString(yr);
    stmp = Integer.toString( mn+1 ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    stmp = Integer.toString( dy ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    stmp = Integer.toString( hr ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    stmp = Integer.toString( min ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    stmp = Integer.toString( sec ); if( stmp.length() < 2){ stmp = "0" + stmp; } nowstring = nowstring + stmp;
    c= null;
    return nowstring;
   }

   
}

最終更新日: 2009-06-03 00:00:00

ともさんのHP >プロブラミング >自作JavaLibrary >時刻を文字列に変換するクラス

広告
新着ページ

AIを利用し、衣服のデザイン画から型紙を制作する方法  
2つのアパレル3D技術でひらくオーダーメイド生産の手法  
【洋裁型紙】前後身頃の肩の傾きは何故前身頃の方が傾いているのか  
電子追尾式天体写真撮影法  
日本ミツバチ巣箱の種類  
ドラフター(製図台)でソーイング  
日本ミツバチが逃亡  
カメさんの箱庭  
天体用デジタルカメラの構造と天体写真  
Javaで静止画像(Jpeg)を動画(Mov)に変換  
USBカメラをJAVAで制御  

他のサイト

3D-CAD
洋裁CAD

いいねなど

 RSS 

Author: Tomoyuki Ito

このサイトの文章・写真の無断転載を禁じます