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?

Ravi Parekh
  • 5,253
  • 9
  • 46
  • 58
Nick Czar
  • 33
  • 1
  • 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? – Chol Nhial Jun 19 '18 at 05:59
  • here **DD** is String variable or something you want to Display as output? – TheSprinter Jun 19 '18 at 06:01
  • 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`. – Dumbo Jun 19 '18 at 06:03
  • Possible duplicate of [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – daniu Jun 19 '18 at 06:22
  • 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 – rayrayray9151 Jun 19 '18 at 06:02

5 Answers5

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());
daniu
  • 14,137
  • 4
  • 32
  • 53
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

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
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));
mah454
  • 1,571
  • 15
  • 38
  • Don't suggest using `java.util.Date` anymore, `java.time.LocalDate` is better in (almost?) every way. – daniu Jun 19 '18 at 06:21
  • https://stackoverflow.com/questions/50921444/java-extract-values-from-a-string/50921883#50921883 – daniu Jun 19 '18 at 06:59
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

Dumbo
  • 1,630
  • 18
  • 33
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;
            }
        }
    }
}

}

Karol Katanowski
  • 176
  • 2
  • 13