将数组对象转换为时间

发布于 2024-10-10 06:53:25 字数 152 浏览 0 评论 0原文

如果我有一个包含字符串 07:46:30 pm 、 10:45:28 pm 、 07:23:39 pmArray ,...... .

我想将其转换为时间。我该怎么做?

If i have an Array which contains the Strings 07:46:30 pm , 10:45:28 pm , 07:23:39 pm , .......

and I want to convert it into Time. How can i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天赋异禀 2024-10-17 06:53:25

以下是将给定格式的字符串数组转换为日期数组的方法:

public static Date[] toDateArray(final String[] dateStrings)
throws ParseException{
    final Date[] arr = new Date[dateStrings.length];
    int pos = 0;
    final DateFormat df = new SimpleDateFormat("K:mm:ss a");
    for(final String input : dateStrings){
        arr[pos++] = df.parse(input);
    }
    return arr;
}

Here's how to convert an array of Strings in the given format to an array of dates:

public static Date[] toDateArray(final String[] dateStrings)
throws ParseException{
    final Date[] arr = new Date[dateStrings.length];
    int pos = 0;
    final DateFormat df = new SimpleDateFormat("K:mm:ss a");
    for(final String input : dateStrings){
        arr[pos++] = df.parse(input);
    }
    return arr;
}
烟燃烟灭 2024-10-17 06:53:25

使用 SimpleDateFormat 类。这是一个例子:

DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
for(String str : array){
    Date date = formatter.parse(str);
}

Use the SimpleDateFormat class. Here is an example:

DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
for(String str : array){
    Date date = formatter.parse(str);
}
不交电费瞎发啥光 2024-10-17 06:53:25

使用 SimpleDateFormat 类来解析日期。

Use the SimpleDateFormat class to parse the date.

时光礼记 2024-10-17 06:53:25

您需要编写自己的解析器。请参阅此示例:

http://www.kodejava.org/examples/101.html

You need to write your own parser. See this example:

http://www.kodejava.org/examples/101.html

↘人皮目录ツ 2024-10-17 06:53:25

如果您想要时间数组或列表...

String[] arrString = new String[] { "07:46:30 pm", "10:45:28 pm", "07:23:39 pm" }

Time[] arrTime = new Time[strArray.lengh];

List<Time> listTime = new ArrayList<Time>();

数组字符串到数组或列表时间:

//For array
for (int i = 0; i < arrString.length; i++) {
   DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
   Date date = formatter.parse(arrString[i]);

   //Populate the ARRAY
   arrTime[i] = new Time(date.getTime());
}

//For list
for (String str : arrString) {
   DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
   Date date = formatter.parse(str);

   //Populate the LIST
   listTime.add(new Time(date.getTime()));
}

If you want a array or list of time...

String[] arrString = new String[] { "07:46:30 pm", "10:45:28 pm", "07:23:39 pm" }

Time[] arrTime = new Time[strArray.lengh];

or

List<Time> listTime = new ArrayList<Time>();

Array string to array or list time:

//For array
for (int i = 0; i < arrString.length; i++) {
   DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
   Date date = formatter.parse(arrString[i]);

   //Populate the ARRAY
   arrTime[i] = new Time(date.getTime());
}

//For list
for (String str : arrString) {
   DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
   Date date = formatter.parse(str);

   //Populate the LIST
   listTime.add(new Time(date.getTime()));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文