简体   繁体   English

如何从字符串日期获取日,月,年?

[英]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();

java.time java.time

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.

相关问题 如何从年,月和日获取日期对象? - How to get a Date object from year,month and day? 从日期字符串中分离年,月和日的巧妙方法? - Clever way to separate year, month and day from date string? 如何在GoogleAppEngine应用中的Java中获取当前日期,以及如何从该日期中分离出Day,Month和Year并将其存储在数据存储区中 - how to get current date in java in GoogleAppEngine app, and separate Day, Month and Year from that date and store it in datastore 从java.sql.date获取日月 - get day month year from java.sql.date 如何在jtextfield中以日月(字)和年的格式获取日期 - How to get date in jtextfield in format of day month(in words) and year 我想从存储在字符串中的日期(例如“ Wed,27 feb 2013”​​)中获取日期名称,日,月,年 - i want to get day name , day , month ,year from the date stored in String like “Wed, 27 feb 2013” Java。 如何从与java.util.Date关联的Jspinner获取月,年,日? - Java. How to get month, year, day from Jspinner which is associated with java.util.Date? 如何从Android中选定的日期月份和年份中获取星期几名称? - How to get day of week name from selected date month and year in Android? 如何从整个日期获得年份和月份? - how to get year and month from a whole date? 在GWT中建立年,月,日的日期 - Building a date from year, month and day in GWT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM