0

This is the code to get date format

GregorianCalendar calDate = new GregorianCalendar();

The constructor will hold value of format as 2015,7,15 what I want to achieve is to be able to set this format from a text field. For example

GregorianCalendar calDate = new GregorianCalendar(Formats.getText());

But I have errors and is not working. Please whats is right code?

Resolved with the below code

String nnhh= ""+Firstname.getText();
                                    String someDate = ""+nnhh;
                                    SimpleDateFormat sdf = new SimpleDateFormat("MM,dd,yyyy");
                                    try {
                                        Date date = sdf.parse(someDate);
                                        long dd=date.getTime();
                                        Firstname.setText(""+dd);} catch (ParseException e) {
                                        e.printStackTrace();
                                    }

and dd was in TimeInMillis just as I wanted. Thanks

Gstuntz
  • 434
  • 4
  • 10

1 Answers1

0

To be able to parse it, I would suggest you to use something like:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("y M d VV m s H");

The formatter requires all of these elements. (see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html for formatting details).

And this should be all you need:

GregorianCalendar calDate = GregorianCalendar.from(ZonedDateTime.parse(dateToParse, formatter));
  • DateTimeFormatter not seen in android – Gstuntz May 11 '16 at 10:42
  • If you use gradle you can add this to the build: compile **'joda-time:joda-time:2.9.3'**. Yes DateTimeFormatter is not available on **Android** but it is also not available to any prior **Java 8** version. You can use it like this with joda time: final GregorianCalendar calDate = new GregorianCalendar(); calDate.setTime(DateTime.parse(dateToParse, formatter).toDate());. Just remember that also the **GregorianCalendar** changes also a bit and that the V pattern doesn't work the same way. – Joao Esperancinha May 11 '16 at 22:28