Sun
Sun

Reputation: 6888

Parse a date from a string

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";

Upvotes: 0

Views: 161

Answers (7)

Basil Bourque
Basil Bourque

Reputation: 338426

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.

Upvotes: 3

RAMA KRISHNA
RAMA KRISHNA

Reputation: 11

  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));

Upvotes: 0

Benoit Vanalderweireldt
Benoit Vanalderweireldt

Reputation: 2989

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));

Upvotes: 5

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

Upvotes: 0

Abhishek Patel
Abhishek Patel

Reputation: 4328

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];

Upvotes: 0

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6846

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.

Upvotes: 0

Shree Krishna
Shree Krishna

Reputation: 8562

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];

Upvotes: 0

Related Questions