-3
String str_date = currentMonth + 1 + "-" + value.toString() + "-" + currentYear;
            DateFormat formatter ; 
            Date date = null ; 
            formatter = new SimpleDateFormat("dd-MMM-yy");
            try {
                date = (Date)formatter.parse(str_date);
            } 
            catch (ParseException e) {
                e.printStackTrace();
            }  
            System.out.println(date);

こんにちは、現在までの文字列を検索しようとしましたが、ほとんどの人がこの種のコードを使用していますが、まだ null を出力しているようです。ありがとうございました。

4

1 に答える 1

0

どちらかを試してください

formatter = new SimpleDateFormat("dd-MM-yyyy");

また

formatter = new SimpleDateFormat("MM-dd-yyyy");

あなたが実際に何を望んでいるかに応じて。コードは、パターンの最初の部分として 1 か月を使用することをお勧めします。

currentMonth + 1それがあなたが望むものであることを確認してください。currentMonthが 12の場合+1、日付は翌年の 1 月に繰り上げられます。

また、 も使い慣れるようにしてくださいLocale。デフォルトのものはあなたのために働くかもしれませんが、他の誰かのコンピュータでは壊れます.

于 2012-12-03T14:40:48.287 に答える