Java 如何从字符串日期获取日、月、年?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36290467/
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
How to get day,month,year from String date?
提问by Gilad Neiger
I have a date on String, like this:
我在字符串上有一个日期,如下所示:
String date="25.07.2007";
String date="25.07.2007";
And I want to divide it to:
我想把它分成:
String day;
String month;
String year;
What is the best way to do this?
做这个的最好方式是什么?
采纳答案by Turtle
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.
在 Java 中拆分字符串的一种方法是使用 .split("regex") 根据提供的模式拆分字符串,返回一个 String 数组。
String [] dateParts = date.split(".");
String day = dateParts[0];
String month = dateParts[1];
String year = dateParts[2];
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.
获取当前日期:您还可以通过更改传递给 SimpleDateFormat 的值来更改日期格式。不要忘记进口。
DateFormat dateFormater = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date date = new Date();
回答by Ritesh
Try this...
尝试这个...
String dateArr[] = date.split(".");
String day = dateArr[0];
String month = dateArr[1];
String year = dateArr[2];
or
或者
String day = date.substring(0,2);
String month = date.substring(3,5);
String year = date.substring(6,10);
To get the current date...
要获取当前日期...
Date currentDate = Calendar.getInstance().getTime();
To get day, month and year from current date..
从当前日期获取日、月和年..
int day = currentDate.get(Calendar.DAY_OF_MONTH);
int month = currentDate.get(Calendar.MONTH);
int year = currentDate.get(Calendar.YEAR);
Month starts at 0 here...
这里的月份从 0 开始...
回答by Basil Bourque
java.time
时间
The modern way is to use the Android versionof the back-portof the java.timeframework built into Java 8 and later.
现代方法是使用Java 8 及更高版本中内置的java.time框架的后向端口的Android 版本。
First, parse the input string into a date-time object. The LocalDate
class represents a date-only value without time-of-day and without time zone.
首先,将输入字符串解析为日期时间对象。该LocalDate
级表示没有时间一天和不同时区的日期,唯一的价值。
String input = "25.07.2007";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM.dd.yyyy" );
LocalDate localDate = LocalDate.parse( input , formatter );
Now you can ask the LocalDate
for its values.
现在你可以询问LocalDate
它的值。
int year = localDate.getYear();
int month = localDate.getMonthValue();
int dayOfMonth = localDate.getDayOfMonth();
You can use the handy Month
enum to localize the name of the month.
您可以使用方便的Month
枚举来本地化月份的名称。
String monthName = localDate.getMonth().getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH );
回答by a.prateek
date.split(".") will not work
Split methods takes regex as parameter. For regex "." means any character. Instead we have to pass "\." as parameter.
String [] dateParts = date.split("\.");
String day = dateParts[0];
String month = dateParts[1];
String year = dateParts[2];
回答by Neeraj Gahlawat
public class DateTest {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
String starDate = "7/12/1995 12:00:00 AM";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
String newDateStr = simpleDateFormat.format(simpleDateFormat.parse(starDate));
String str[] = newDateStr.split("/");
String month = str[1];
String year = str[2];
}
}