この記事では、Javaでの時間の取得とフォーマットについて説明します。
1.現在のシステムの時間と日付を取得し、出力をフォーマットします。
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:mm:ss");日付()は現在のシステム時間を取得することです}}2。データベースの日付は、前日モードでのみ出力されます。次の2つの方法を使用できます。
1。convert()を使用して関数を変換します。
string sqlst = "select convert(varchar(10)、bookdate、126)は、「2007-4-10」と '2007-4-25'"; system.out.println(rs.getString( "Convertbookdate"))の間にあるBookdateの間にあるルームブックからの変換bookbookdateとしてselect convert(varchar(10)、Bookdate、126);
2。SimpleDateFormatクラスを使用します。
まず、2つのJavaパッケージを入力する必要があります。
Import Java.util.date;
java.text.simpledateformatをインポートします。
次に、日付形式を定義します。
SimpleDateFormat SDF = new SimpledateFormat(YY-MM-DD);
SQLステートメントは次のとおりです。
string sqlstr = "'2007-4-10' 'と' 2007-4-25 '"の間にあるBookDateがあるRoombookからBookDateを選択します。
出力:
system.out.println(df.format(rs.getDate( "bookdate")));
Javaで現在の日付と時刻を取得する方法
Import Java.util.date; java.util.calendarをインポートします。 java.text.simpledateformatをインポートします。 public class testdate {public static void main(string [] args){date now = new date(); simpledateFormat dateformat = new SimpleDateFormat( "yyyy/mm/dd hh:mm:ss"); //日付形式の文字列hehe = dateformat.format(now); System.out.println(hehe);カレンダーc = calendary.getInstance(); // int year = c.get(calendar.year); int month = c.get(calendar.month); int date = c.get(calendar.date); int hour = c.get(calendar.hour_of_day); int minute = c.get(calendar.minute); int second = c.get(calendar.second); System.out.println(year + "/" + month + "/" + date + "" + hour + ":" + minute + ":" + second); }}文字列タイプの時間を日付型に変換する必要がある場合があります。次の方法で、日付のタイプに到達した時間の文字列を変換できます。
SimpleDateFormat SDF = new SimpledateFormat( "yyyy-mm-dd"); java.util.date time = null; try {time = sdf.parse(sdf.format(new Date()));} catch(parseexception e){e.printstacktrace();};}読んでくれてありがとう、私はそれがあなたを助けることができることを願っています。このサイトへのご支援ありがとうございます!