简体   繁体   中英

How to set date and time in a specific format

Usibg this code I get a java.lang.IllegalArgumentException: DTSTART cannot be empty . This error is coming from:

code:

Calendar calender = Calendar.getInstance();
                ContentResolver cr = getContentResolver();
                ContentValues values = new ContentValues();
                String eventTime="12:00:AM 11:59:PM";
                String[] time=eventTime.split(" ");
                String[] execttime=time[0].split(":");
                String eventStartDate="06 Feb 2014"+" " + execttime[0]+":"+execttime[1]+":00";
                long startDate=getDateCurrentMillis(eventStartDate);
                values.put(CalendarContract.Events.DTSTART,
                        startDate);
                Uri EVENTS_URI = Uri.parse(getCalendarUriBase(EventDetail.this)+ "events");

    public static long getDateCurrentMillis(String startDate) {

        SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",

        Locale.getDefault());

        sfd.setTimeZone(TimeZone.getTimeZone("UTC"));

        try {

            Date d = sfd.parse(startDate);

            return d.getTime();

        } catch (Exception e) {

            System.out.println("");

        }    
    return 0;
        }

Try this:

DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, h:mm a");
String date = df.format(Calendar.getInstance().getTime());

The issue is happening in your getDateCurrentMillis method. Is it not able to parse the date because of incorrect format specified in SimpleDateFormat , which result in returning 0 all the time. Passing 0 in DTSTART crashing your app.

It seems your eventStartDate is in format like this

String date = "06 Feb 2014 12:00:00";

So make your SimpleDateFormat object in a way like this

SimpleDateFormat sfd = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.getDefault());

Let me know if I observed your date format correctly, If not print it before passing and make changes accordingly in your SimpleDateFormat object or post in comment I will make necessary changes.

 //to pick date 
  DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
        }
        }



//to pick time

    TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            myCalendar.set(Calendar.MINUTE, minute);
            updateLabel();
        }
        };

for more information use this link

http://www.java-samples.com/showtutorial.php?tutorialid=1520

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM