拆分日期/时间字符串 [英] Split date/time strings
问题描述
我有下载有关一个人的日历事件信息的REST服务...
I have a ReST service which downloads information about events in a persons calendar...
当它返回的日期和时间,则返回它们作为一个字符串
When it returns the date and time, it returns them as a string
例如。日期=2012年12月8日与&时间=上午11:25
e.g. date = "12/8/2012" & time = "11:25 am"
为了把这个到Android的Google日历,我需要做到以下几点:
To put this into the android calendar, I need to do the following:
Calendar beginTime = Calendar.getInstance();
beginTime.set(year, month, day, hour, min);
startMillis = beginTime.getTimeInMillis();
intent.put(Events.DTSTART, startMillis);
如何能我分裂的日期和时间变量,从而使他们在beginTime.set()方法可用?
How can I split the date and time variables so that they are useable in the "beginTime.set() " method?
推荐答案
我不认为你真的需要如何分割字符串,你的情况应该是如何让时间从日期字符串毫秒,在这里有一个例子:
I don't thinks you really need how to split the string, in your case it should be 'how to get time in milliseconds from date string', here is an example:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
String date = "12/8/2012";
String time = "11:25 am";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
try {
Date dt = df.parse(date + " " + time);
Calendar ca = Calendar.getInstance();
ca.setTime(dt);
System.out.println(ca.getTimeInMillis());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
这篇关于拆分日期/时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!