"YYYY-mm-dd hh:mm"
フォーマッタ オブジェクトとして日付形式を取得しています。
入力フォーマッタオブジェクトをフォーマットして取得のみするにはどうすればよい"YYYY-mm-dd";
ですか?
"YYYY-mm-dd hh:mm"
フォーマッタ オブジェクトとして日付形式を取得しています。
入力フォーマッタオブジェクトをフォーマットして取得のみするにはどうすればよい"YYYY-mm-dd";
ですか?
フォーマッターオブジェクトとして「YYYY-mm-ddhh:mm」として日付フォーマットを取得しています。「YYYY-mm-dd」のみを取得するように入力フォーマッタオブジェクトをフォーマットするにはどうすればよいですか。
YYYY-mm-ddとして日付を設定することはできません。yyyy-MM- ddである必要があります。yyyy-MM-ddで日付を取得するには、次のコードを使用します。
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date date;
try {
date = (Date)((DateFormat) formatter).parse("2011-04-13 05:00");
formatter = new SimpleDateFormat("yyyy-MM-dd");
String s = formatter.format(date);
System.out.println(s);
} catch (ParseException e) {
e.printStackTrace();
}
使用するSimpleDateFormat
String myDateString = "2009-04-22 15:51";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(outFormat.format(inFormat.parse(myDateString)));
この形式で日付を取得していて、それが必要な場合は、"YYYY-mm-dd hh:mm"
を"YYYY-mm-dd"
使用することをお勧めしますinputDate.substring(0, 10)
。
いずれにせよ、潜在的なY10kバグに注意してください:)
はい、SimpleDateFormat はあなたが探しているものです http://dlc.sun.com.edgesuite.net/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/latest/html/en/api/java /text/SimpleDateFormat.html
これを試して:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
SimpleDateFormat
あなたが探しているものです。
この質問にはたくさんの良い答えがあります!! 、ここにもう1つのより一般的なソリューションがあります
public static String getDateInFormate(String oldFormate , String newFormate , String dateToParse){
//old "yyyy-MM-dd hh:mm"
//new yyyy-MM-dd
//dateTopars 2011-04-13 05:00
String formatedDate="";
Format formatter = new SimpleDateFormat();
Date date;
try {
date = (Date)((DateFormat) formatter).parse(dateToParse);
formatter = new SimpleDateFormat(newFormate);
formatedDate = formatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return formatedDate;
}
次のコードを使用します。
Date date=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
System.out.println("formatted time==>" + formattedDate);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
日付と時刻の作業には、Java の最新の日付と時刻 API である java.time を使用することをお勧めします。
入力を解析するには、フォーマッタを定義します。
private static final DateTimeFormatter FORMATTER
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm", Locale.ROOT);
解析:
String input = "2019-01-21 23:45";
LocalDateTime dateTime = LocalDateTime.parse(input, FORMATTER);
System.out.println(dateTime);
これまでの出力:
2019-01-21T23:45
フォーマット出力:
String output = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(output);
2019-01-21
Trail: Date Time (Java™ チュートリアル) では、java.time の使用方法を説明しています。
user2663609 によるものなどの他の回答は正しいです。
別の方法として、java.util.Date/Calendar クラスの第 3 部のオープンソース代替物であるJoda-Timeには、必要に応じて組み込み形式が含まれています。
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
String stringIn = "2011-04-07";
// Returns a formatter for a full date as four digit year, two digit month of year, and two digit day of month (yyyy-MM-dd).
DateTimeFormatter formatter = ISODateTimeFormat.date().withZone( DateTimeZone.forID( "Europe/London" ) ).withLocale( Locale.UK );
DateTime dateTime = formatter.parseDateTime( stringIn ).withTimeAtStartOfDay();
String stringOut = formatter.print( dateTime );
コンソールにダンプ…</p>
System.out.println( "dateTime: " + dateTime.toString() );
System.out.println( "stringOut: " + stringOut );
実行すると…</p>
dateTime: 2011-04-07T00:00:00.000+01:00
stringOut: 2011-04-07