A R
A R

Reputation: 425

separating a string in to 3 different string variables in java

I have a String variable as a date type.for example: String myDate= "20130403"; and I want to separate it to 3 different date formats like this:

String myDay = "03";
String myMonth= "04";
String myYear= "2013";

how is the fastest way to separate my String?

thanks

Upvotes: 2

Views: 751

Answers (9)

Punith Raj
Punith Raj

Reputation: 2195

substring might perform the task, but is not the fastest way. Substring creates new string object for all the tokens, hence not performance efficient or the fastest way. below is the source code for subString() from java src.

public String More ...substring(int beginIndex, int endIndex) {
 if (beginIndex < 0) {
     throw new StringIndexOutOfBoundsException(beginIndex);
 }
 if (endIndex > count) {
     throw new StringIndexOutOfBoundsException(endIndex);
 }
 if (beginIndex > endIndex) {
     throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
 }
 return ((beginIndex == 0) && (endIndex == count)) ? this :
     **new String(offset + beginIndex, endIndex - beginIndex, value);**

}

so, the best way is to, split using the regEx according to me.

regards Punith

Upvotes: 0

ba3a
ba3a

Reputation: 340

I think you'd like to use special java simple date format for this.

Upvotes: 2

initramfs
initramfs

Reputation: 8405

If the string has a fixed length (e.g the year is always 4 digits, the month is always 2, etc) you can use the substring() method.

In your case:

String myDate = "20130403";
String myYear = myDate.substring(0,4);
String myMonth = myDate.substring(4,6);
String myDay = myDate.substring(6,8);

If the string is not fixed length you may try using a delimiter e.g "-". So your string looks like this "2013-04-03" and then simply use the String.split(); function.

Example:

String myDate = "2013-04-03";
String[] myDateElements = myDate.split("-");
String myYear = myDateElements[0];
String myMonth = myDateElements[1];
String myDay = myDateElements[2];

Upvotes: 2

Rollyng
Rollyng

Reputation: 1417

String yourDateString = "20130403";
SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
Date currentDate = sd.parse(yourDateString);

Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
String year = String.valueOf(cal.get(Calendar.YEAR)) + "";

// As Gyro said month start at 0 to 11.
String month = String.valueOf(cal.get(Calendar.MONTH)+1) + "";  

String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH)) + "";

Upvotes: 6

francois.robert
francois.robert

Reputation: 61

Are you try substring ? like this (best way i think and fastest too) :

@Test
final public void testStringDateOperation() {
    final String myDate= "20130403";
  final String year = myDate.substring(0, 4);
    final String month = myDate.substring(4, 6);
    final String day = myDate.substring(6, 8);
    Logger.trace("year: {0} - month: {1} - day: {2}", year, month, day);
}

Upvotes: 2

Amin Abu-Taleb
Amin Abu-Taleb

Reputation: 4511

You can use SimpleDateFormat to parse your String as date and, using Calendar, get all fields you want.

    String myDate= "20130403";

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    try {
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(myDate));

        String myDay = (String)cal.get(Calendar.DATE);
        String myMonth = (String)(cal.get(Calendar.MONTH)+1);
        String myYear = (String)cal.get(Calendar.YEAR);
    } catch (ParseException e) {
        e.printStackTrace();
    }

Upvotes: 3

nkukhar
nkukhar

Reputation: 2025

If you know for sure that your date comes with such format yyyyMMdd then you can use subString function for this

String myDate = "20120221"

String year = myDate.subString(0,4);
String month = myDate.subString(4,6);
String day = myDate.subString(6);

Upvotes: 3

Abubakkar
Abubakkar

Reputation: 15654

You can use substring if your data string format isn't going to change:

String myDate= "20130403";

String myDay = myDate.substring(6,8);
String myMonth = myDate.substring(4,6);
String myYear = myDate.substring(0,4);

Upvotes: 2

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

If this is a fixed format simply use substring.

    String myDate= "20130403";
    String myYear = myDate.substring(0,4);
    String myMonth= myDate.substring(4,6);
    String myDay= myDate.substring(6,8);

    System.out.println(myDay);
    System.out.println(myMonth);
    System.out.println(myYear);

Upvotes: 4

Related Questions