0

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";
Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31
Sun
  • 6,768
  • 25
  • 76
  • 131
  • You have to use SimpleDateFormat https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – JChap Apr 06 '16 at 04:37

7 Answers7

5

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));
Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31
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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

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];
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
0

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.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
0

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];
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
  • Even though it provides the right answer. I do not think is the proper way of handling Dates. It should be handled with a DateFormat or SimpleDateFormat. – Gonzalo Matheu Aug 19 '16 at 19:48
0

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

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0
  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));