Java 拆分日期/时间字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18099285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:22:41  来源:igfitidea点击:

Split date/time strings

javaandroidandroid-calendar

提问by Louis Evans

I have a ReST service which downloads information about events in a persons calendar...

我有一个 ReST 服务,它下载有关个人日历中事件的信息......

When it returns the date and time, it returns them as a string

当它返回日期和时间时,将它们作为字符串返回

e.g. date = "12/8/2012" & time = "11:25 am"

例如日期 = "12/8/2012" & 时间 = "11:25 am"

To put this into the android calendar, I need to do the following:

要将其放入 android 日历,我需要执行以下操作:

Calendar beginTime = Calendar.getInstance();
beginTime.set(year, month, day, hour, min);
startMillis = beginTime.getTimeInMillis();
intent.put(Events.DTSTART, startMillis);

How can I split the date and time variables so that they are useable in the "beginTime.set() " method?

如何拆分日期和时间变量,以便它们在“beginTime.set()”方法中可用?

采纳答案by Harry.Chen

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();
        }
    }
}

回答by Canberk

You can see an example of split method here : How to split a string in Java

您可以在此处查看拆分方法的示例:How to split a string in Java

Then simply use the array for your parameter eg: array[0] for year and etc..

然后只需将数组用于您的参数,例如:array[0] 表示年份等。

回答by Jazib

Assuming you get your date in String format (if not, convert it!) and then this:

假设您以字符串格式获取日期(如果没有,请转换它!)然后:

String date = "12/8/2012";
String[] dateParts = date.split("/");
String day = dateParts[0]; 
String month = dateParts[1]; 

Similarly u can split time as well!

同样,你也可以分割时间!

回答by MysticMagic?

Try this:

尝试这个:

String date = "12/8/2012";
String time = "11:25 am";

String[] date1 = date.split("/");
String[] time1 = time.split(":");
String[] time2 = time1[1].split(" ");  // to remove am/pm

Calendar beginTime = Calendar.getInstance();
beginTime.set(Integer.parseInt(date1[2]), Integer.parseInt(date1[1]), Integer.parseInt(date1[0]), Integer.parseInt(time1[0]), Integer.parseInt(time2[0]));
startMillis = beginTime.getTimeInMillis();
intent.put(Events.DTSTART, startMillis);

Hope this helps.

希望这可以帮助。

回答by Antoniossss

Use SimpleDateFormat(check api docs). If you provide proper time pattern it will be able to convert string into Date instantly.

使用SimpleDateFormat(检查 api 文档)。如果您提供适当的时间模式,它将能够立即将字符串转换为日期。

回答by Ruchira Gayan Ranaweera

This is just a Idea, you can do some thing like this without splitting

这只是一个想法,你可以在不分裂的情况下做这样的事情

    DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm a");
    Date date = formatter.parse("12/8/2012 11:25 am");      
    Calendar cal=Calendar.getInstance();
    cal.setTime(date);