1

これは電話の日付を取得する方法ですが、parseException が表示されます。何が問題なのですか?

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String date = sdf.format(new Date());
    Date localDate = sdf.parse(date);
4

4 に答える 4

3

new Date(0)は現在の日付/時刻ではありません。を使用する必要がありますnew Date()。その後、 ParseException はなくなるはずです。なぜそれが得られたのか知りたい場合は、プログラムをデバッグして、 new Date(0) が文字列として与えるものを見てください。解析に失敗した理由がわかります。

Date now = new Date();
Date alsoNow = Calendar.getInstance().getTime();
String nowAsString = new SimpleDateFormat("yyyy-MM-dd").format(now);

それはうまくいきます。そしてそれも:

Date christmas = new SimpleDateFormat("yyyy-MM-dd").parse("2012-12-25");

ところで、使用していることと使用java.util.Dateしていないことを確認してくださいjava.sql.Date

于 2012-10-20T22:19:40.647 に答える
1
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
String nowDate = formatter.format(now.getTime());
String[] separateCurrentDate = nowDate.split("-");
String year = separateCurrentDate[0];
String month = separateCurrentDate[1];
String day = separateCurrentDate[2];
int currentYear = Integer.parseInt(year);
int currentMonth = Integer.parseInt(month);
int currentDay = Integer.parseInt(day);

次に、y、m、dを1つずつDate型のonjectに格納します

于 2012-10-20T23:21:30.390 に答える
1
 Date now = new Date();
   Date alsoNow = Calendar.getInstance().getTime();
   String nowAsString = new SimpleDateFormat("yyyy-MM-dd").format(now);
   currentdate = (TextView)findViewById(R.id.textcntdate);
   currentdate.setText(nowAsString);
于 2015-07-08T06:39:30.033 に答える
-1
Date dt = new Date();
                    int hours = dt.getHours();
                    int minutes = dt.getMinutes();
                    int seconds = dt.getSeconds();
                    String curTime = hours + ":" + minutes + ":" + seconds;

そして、ここに別のリンクがあります この Display the current time and date in a Android application

于 2012-10-20T22:21:25.990 に答える