[英]How to get day,month,year from String date?
I have a date on String, like this: 我在String上有一个日期,如下所示:
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? 做这个的最好方式是什么?
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. 获取当前日期:您还可以通过更改传递给SimpleDateFormat的值来更改日期的格式。 Don't forget the imports.
不要忘记进口。
DateFormat dateFormater = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date date = new Date();
The modern way is to use the Android version of the back-port of the java.time framework built into Java 8 and later. 现代的方法是使用Java版及更高版本内置的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
枚举来本地化Month
的名称。
String monthName = localDate.getMonth().getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH );
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开始......
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];
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];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.