如何在android日历中添加日期?

时间:2014-11-28 23:06:08

标签: android json calendar android-calendar

如何在android日历中添加以下日期?我尝试将我的日期字符串转换为SimpleDateFormat。但我仍然不知道如何将其添加为beginTime。

2014-11-24 16:25:55+0530

有人请帮忙......

String strDate = "2014-11-24 16:25:55+0530";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss' 'Z");

Date convertedDate = new Date();
try {
    convertedDate = dateFormat.parse(datetime);
    SimpleDateFormat sdfmonth = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat sdftime = new SimpleDateFormat("HH:mm:ss a");


    monthday = sdfmonth.format(convertedDate);
    time = sdftime.format(convertedDate);


    Toast.makeText(getApplicationContext(),"Date "+monthday, 
            Toast.LENGTH_LONG).show();

    Toast.makeText(getApplicationContext(),"Time  "+time, 
            Toast.LENGTH_LONG).show();

} catch (ParseException e) {
    e.printStackTrace();
}

Calendar beginTime = Calendar.getInstance();
beginTime.set(eventdate);
Intent intent = new Intent(Intent.ACTION_INSERT)
    .setData(Events.CONTENT_URI)
    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
    .putExtra(Events.TITLE, eventName)
    .putExtra(Events.EVENT_LOCATION, location);
startActivity(intent);

1 个答案:

答案 0 :(得分:0)

您可以转换日期字符串,即

String strDate = "2014-11-24 16:25:55 +0530"; 

到Date,然后使用以下代码以毫秒为单位:

 try {

      String strDate = "2014-11-24 16:25:55 +0530";

      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss' 'Z");

      Date d = dateFormat.parse(strDate);

      long millisec = d.getTime();

      System.out.println("Millisec :" + millisec);

      } catch (ParseException e) {

        }

希望这有助于:)