如何从字符串日期获取日,月,年? [英] How to get day,month,year from String date?

查看:170
本文介绍了如何从字符串日期获取日,月,年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在String上有个约会,像这样:

I have a date on String, like this:

字符串date ="25.07.2007";

我想将其划分为:

String day;
String month;
String year;

做到这一点的最佳方法是什么?

What is the best way to do this?

推荐答案

在Java中拆分字符串的一种方法是使用.split("regex"),该方法根据提供的模式拆分字符串,并返回String数组.

One way to split a string in Java is to use .split("regex") which splits a string according to the pattern provided, returning a String array.

String [] dateParts = date.split(".");
String day = dateParts[0];
String month = dateParts[1];
String year = dateParts[2];

要获取当前日期:您还可以通过更改传递给SimpleDateFormat的值来更改日期的格式.不要忘记进口.

To get current date: You can also change the format of the date by changing the values passed to SimpleDateFormat. Don't forget the imports.

  DateFormat dateFormater = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
  Date date = new Date();

这篇关于如何从字符串日期获取日,月,年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆