1

This is how i get the phone's date, but it prompts me with a parseException, what's the problem?

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String date = sdf.format(new Date());
    Date localDate = sdf.parse(date);
Blake
  • 7,367
  • 19
  • 54
  • 80

4 Answers4

3

new Date(0) is not the current date/time. You should use new Date(). ParseException should go away then. If you wanna know why you got that, simply debug your program and have a look at what new Date(0) gives as a String, you'll know why it fails to be parsed.

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

That works. And that too:

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

By the way, make sure you are using java.util.Date and not java.sql.Date

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
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);

and then store y,m,d one by one into a Date type onject

Blake
  • 7,367
  • 19
  • 54
  • 80
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);
-1
Date dt = new Date();
                    int hours = dt.getHours();
                    int minutes = dt.getMinutes();
                    int seconds = dt.getSeconds();
                    String curTime = hours + ":" + minutes + ":" + seconds;

And then also here is another link on this Display the current time and date in an Android application

Community
  • 1
  • 1
Tony
  • 4,609
  • 2
  • 22
  • 32