2

I'm new to Java and am trying to extract values from a date-related user input string and split it into a DD-MM-YYYY format.

I tried to use the code below:

String inputString = "s.param1"; 
String[] arraySplit_3 = inputString.split("-");
for (int i=0; i < arraySplit_3.length; i++){
    System.out.println(arraySplit_3[i]);
}

s.param1 gets the input of the user, I use a separate excel file for it.

if s.param1 = 15-05-2010

I wish to get this output:

DD: 15

MM: 05

YYYY: 2010

Is there a way to create a method like this?

5
  • 1
    I ran your code, and I got 15, 05, 2010 when I substituted s.param1 for 15-05-2010. Seems to be working fine, what's the issue? Commented Jun 19, 2018 at 5:59
  • here DD is String variable or something you want to Display as output? Commented Jun 19, 2018 at 6:01
  • You can try exploring the docs of SimpleDateFormat of java. Or you can alternatively do the manual way by using substring to extract the different parts Commented Jun 19, 2018 at 6:02
  • If s.param1 is variable so you shouldn't use it in quotes. Otherwise it will be String. But your created method should work when you set date instead of s.param1. Commented Jun 19, 2018 at 6:03
  • Possible duplicate of Java string to date conversion Commented Jun 19, 2018 at 6:22

5 Answers 5

2

Use java.time.LocalDate to parse this.

// dd: Day of Month
// MM: Month of year
// yyyy: year, four digits
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(inputString);
System.out.printf("DD: %d%nMM: %d%nYYYY: %n", 
    date.getDayOfMonth(), date.getMonthValue(), date.getYear());
Sign up to request clarification or add additional context in comments.

Comments

1

If your s.param1 is variable which gets date in String, so you shouldn't use it in quotes. Otherwise it will be String. And variable name could not be with dot. It could be sParam1.
But when you set date instead of s.param1 your created method should work.

// input "15-05-2010"
String inputString = sparam1; 
String[] arraySplit_3 = inputString.split("-");
for (int i=0; i < arraySplit_3.length; i++){
    System.out.println(arraySplit_3[i]);
}

The output will be:
15
05
2010

If you want to add some chars before the numbers don't use for loop. Use it like this:

// ...
if (arraySplit_3 > 2) {
    System.out.println("DD: " + arraySplit_3[0]);
    System.out.println("MM: " + arraySplit_3[1]);
    System.out.println("YYYY: " + arraySplit_3[2]);
}

Then output will be:
DD: 15
MM: 05
YYYY: 2010

2 Comments

Yes, s.param1 is a variable. I had a mistake by adding the double quotes to it. Your method perfectly worked for me.
@NickCzar , glad I could help you.
1

You are almost there, but maybe do not bother using a loop

String inputString = "15-05-2010"; 
String[] arraySplit_3 = inputString.split("-");
System.out.println("DD: " + arraySplit_3[0]);
System.out.println("MM: " + arraySplit_3[1]);
System.out.println("YYYY: " + arraySplit_3[2]);

But if you are wanting something more exotic look for SimpleDateFormat

Comments

1

Why you don't pars to date/calendar Object ?

String input = "15-05-2010";
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Date date = format.parse(input);
Calendar calendar = Calendar.getInstance() ;
calendar.setTime(date);
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH));
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));

2 Comments

Don't suggest using java.util.Date anymore, java.time.LocalDate is better in (almost?) every way.
0

If you want to use a loop for that i would suggest You to create spearated method for splitting values here i have an example code

public class licz {
public static void main(String[] args) {

    List<String> list = new ArrayList<String>();
    list.add("15-05-2010");
    list.add("18-02-2015");
    extractValues(list);
}

private static void extractValues(List<String> list) {
    int change = 0;
    for (int i=0; i < list.size(); i++) {
        String[] split = list.get(i).split("-");
        for (String splitElement : split) {
            switch (change) {
                case 0:
                    System.out.println("DD: " + split[0]);
                    change++;
                    break;
                case 1:
                    System.out.println("MM: " + split[1]);
                    change++;
                    break;
                case 2:
                    System.out.println("YYYY: " + split[2]);
                    change=0;
                    break;
            }
        }
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.