Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/users/2/moriya-h5/web/confrage/java/date/chartodate/chartodate.html on line 2

Warning: include_once(http://www.confrage.com/bin/header.php): failed to open stream: no suitable wrapper could be found in /home/users/2/moriya-h5/web/confrage/java/date/chartodate/chartodate.html on line 2

Warning: include_once(): Failed opening 'http://www.confrage.com/bin/header.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/date/chartodate/chartodate.html on line 2
文字列から日付に変換 | Java | 株式会社CONFRAGE
Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/date/chartodate/chartodate.html on line 17

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/date/chartodate/chartodate.html on line 17
文字列から日付に変換
Javaで文字列から日付に変換します。以下は今日ならHH:mm形式、今日以外ならyyyy/MM/dd形式の日付を返す例です。

package purejava;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Sample {

  public static void main(String[] args) {
    String dt = "2014030801020000";
    
    System.out.println(isToday(dt));
  }
  /**
   * 
   * @param dt yyyyMMddhhssSSS形式の日付
   * @return 今日の場合hh:ss形式、今日以外の場合yyyy/MM/dd形式の日付を返す
   */
  public static String isToday(String dt){
    dt = dt.substring(0,12);
    int yyyy = Integer.parseInt(dt.substring(0,4));
    int MM   = Integer.parseInt(dt.substring(4,6));
    int dd   = Integer.parseInt(dt.substring(6,8));
    int mm   = Integer.parseInt(dt.substring(8,10));
    int ss   = Integer.parseInt(dt.substring(10,12));
    
    Calendar calendar = Calendar.getInstance();   
      String today = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
     
      dt.substring(0, 12);
      
      calendar.set(yyyy, MM, dd,mm,ss);
      if(dt.substring(0, 8).equals(today)){
        return dt.substring(8,10) + ":" + dt.substring(10,12);
      }else{
        return dt.substring(0,4) + "/" + dt.substring(4,6) + "/" + dt.substring(6,8);
      }
  }
}

Back to top

Information

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/date/chartodate/chartodate.html on line 96

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/date/chartodate/chartodate.html on line 96

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/date/chartodate/chartodate.html on line 105

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/date/chartodate/chartodate.html on line 105