この記事の例では、昨日の日付文字列を取得するためのJavaの方法を示しています。次のように、参照のために共有してください。
Import java.text.simpledateformat; Import Java.util.date; public class test { / ** *昨日の日付文字列を取得 * * @param nowdate * string of the current date * @return * / public static string getlastdayinfo(string nowdate){string昨日= ""; int year = 0; int month = 0; int day = 0; try {year = integer.parseint(nowdate.substring(0、nowdate.indexof( " - ")));月= integer.parseint(nowdate.substring(nowdate.indexof( " - ") + 1、nowdate.lastindexof( " - "))); day = integer.parseint(nowdate .substring(nowdate.lastindexof( " - ") + 1)); // system.out.println( "nowyear =" + year); // system.out.println( "nowmonth =" + month); // system.out.println( "dayday =" + day); Day = Day -1; if(day == 0){月=月-1; if(month == 0){// 1月月= 12;日= 31;年=年-1; } else {// 1月のswitch(月){// 1 | 3 | 5 | 7 | 8 | 10 | 12)day = 31 ;;ケース1:日= 31;壊す;ケース3:日= 31;壊す;ケース5:日= 31;壊す;ケース7:日= 31;壊す;ケース8:日= 31;壊す;ケース10:日= 31;壊す;ケース10:日= 31;壊す;ケース7:日= 31;壊す;ケース8:日= 31;壊す;ケース10:日= 31;壊す;ケース12:日= 31;壊す; // 4 | 6 | 9 | 11)day = 30 ;;ケース4:日= 30;壊す;ケース6:日= 30;壊す;ケース9:日= 30;壊す;ケース11:日= 30;壊す;ケース2:if(year%4 == 0 && year%100!= 0 || year%400 == 0){// leap year day = 29; } else day = 28; }}} string monthstr = "";文字列daystr = ""; if(month <10){monthstr = "0" + string.valueof(month); } else {monthstr = string.valueof(month); } if(day <10){daystr = "0" + string.valueof(day); } else {daystr = string.valueof(day); }昨日= string.valueof(year) + " - " + monthstr + " - " + daystr; } catch(Exception e){e.printstacktrace(); }昨日戻ってきます。 } public static void main(string [] args)throws exception {system.out.println(getLastDayInfo( "2008-03-01")); System.out.println(getLastDayInfo( "2008-01-01")); SimpleDateFormat sdf = new SimpledateFormat( "mmmm-dd-yyy");日付日;文字列dateTime = "02-14-1984"; try {date = sdf.parse(datetime); System.out.println(sdf.format(date)); } catch(Exception e){System.out.println(e.getMessage()); } date d = java.sql.date.valueof( "2008-07-06"); system.out.println(d.toString()); }}Java関連のコンテンツの詳細については、このサイトのトピックをご覧ください:「Javaファイルの概要とディレクトリ操作スキル」、「Javaデータ構造とアルゴリズムに関するチュートリアル」、「Java操作の操作スキルの概要」、「Java Cache操作スキルの要約」
この記事がみんなのJavaプログラミングに役立つことを願っています。