JAvaで現在のシステム時間、日付、出力をフォーマット

1198 ワード

一.現在のシステム時間と日付を取得し、出力をフォーマットする.
import java.util.Date; 
import java.text.SimpleDateFormat;
public class NowString { 
   public static void main(String[] args) {      
   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//      
      System.out.println(df.format(new Date()));// new Date()         
   } 
}

二.データベース内の日付は年-月-日のみ出力され、次の2つの方法があります.
1、convert()で関数を変換する:
String sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate  between '2007-4-10' and '2007-4-25'";
System.out.println(rs.getString("convertBookDate"));

2、SimpleDateFormat類を利用する:
まずjavaパッケージを2つ入力します.
import java.util.Date; 
import java.text.SimpleDateFormat;

次に、
日付のフォーマットを定義するには、次の手順に従います.
SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);

sql文は次のとおりです.
String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

出力:
System.out.println(df.format(rs.getDate("bookDate")));