Parse a date from a string

眉间皱痕 提交于 2019-12-12 02:51:43

问题


I have String like this:

String strDateTimeStamp = "2016-02-29 18:31:51";

Now I would like to extract it to get result in a below format:

String strYear = "2016";
String strMonth = "02";
String strDate = "29";
String strHour = "18";
String strMinute = "31";
String strSecond = "51";

回答1:


Try This..

    String CurrentString = "2016-02-29 18:31:51";
    String[] separated = CurrentString.split(" ");
    String date = separated[0];
    String time = separated[1];
    String[] separated_date = date.split("-");
    String[] separated_time = time.split(":");
    String strYear = separated_date[0];
    String strMonth = separated_date[1];
    String strDate = separated_date[2];
    String strHour = separated_time[0];
    String strMinute = separated_time[1];
    String strSecond = separated_time[2];



回答2:


If you are working with dates you should consider using Calendar :

    String strDateTimeStamp = "2016-02-29 18:31:51";
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date date = sdf.parse(strDateTimeStamp);
    Calendar cal = new Calendar.Builder().setInstant(date).build();

    String strYear = Integer.toString(cal.get(Calendar.YEAR));
    // Calendar MONTH is starting from 0 we need to add 1
    String strMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
    String strDate = Integer.toString(cal.get(Calendar.DATE));

    String strHour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY));
    String strMinute = Integer.toString(cal.get(Calendar.MINUTE));
    String strSecond = Integer.toString(cal.get(Calendar.SECOND));



回答3:


All of the Answers using java.util.Date and java.text.DateFormat/.SimpleDateFormat are outmoded. Those old date-time classes are poorly designed, confusing, and troublesome. Avoid them.

java.time

The java.time framework is built into Java 8 and later. A vast improvement over the old date-time classes.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

First, replace the SPACE in the middle of your input string with a T to conform with the ISO 8601 standard. These standard formats are used by default in the java.time classes when parsing/generating strings.

String input = "2016-02-29 18:31:51".replace( " " , "T" );

Parse as a LocalDateTime. The “Local” means not associated with any time zone. So this is not an actual moment on the timeline. But apparently not an issue in the context of this Question.

LocalDateTime ldt = LocalDateTime.parse( input );

Now you can ask for your various pieces as needed by calling the various getter methods. These methods return an int primitive which you can, of course, convert to String values.

  • getYear
  • getMonthValue (or getMonth for Month enum))
  • getDayOfMonth (and getDayOfWeek for DayOfWeek enum)
  • getHour
  • getMinute
  • getSecond
  • getNano (the fraction of a second)



回答4:


You can do like this by splitting your String

String[] splittedString = strDateTimeStamp.split("-|:|\\s");

String strYear = splittedString[0];
String strMonth = splittedString[1];
String strDate = splittedString[2];
String strHour = splittedString[3];
String strMinute = splittedString[4];
String strSecond = splittedString[5];



回答5:


First split string using split(" ") on the basis of space ..it will give you a array of string of length 2 . which contains (2016-03-04) and (16:32:33) . Then split both string againg using split("-") and split(":") reapectively . you will get your answer. Please try code at your own may better to you.




回答6:


I suggest to use regex with a pattern like "[- :]"

Example:

public static void main(String[] args) {
    String strDateTimeStamp = "2016-02-29 18:31:51";
    String[] solution = strDateTimeStamp.split("[- :]");
    for (int i = 0; i < solution.length; i++) {
        System.out.println(solution[i]);
    }
}

this will generate an array with all the elements you need




回答7:


  String strDateTimeStamp = "2016-02-29 18:31:51";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = sdf.parse(strDateTimeStamp);
Calendar cal = new Calendar.Builder().setInstant(date).build();

String strYear = Integer.toString(cal.get(Calendar.YEAR));
// Calendar MONTH is starting from 0 we need to add 1 
String strMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
String strDate = Integer.toString(cal.get(Calendar.DATE));

String strHour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY));
String strMinute = Integer.toString(cal.get(Calendar.MINUTE));
String strSecond = Integer.toString(cal.get(Calendar.SECOND));


来源:https://stackoverflow.com/questions/36441504/parse-a-date-from-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!