-2

I have a String which is formatted like so: "dd-MM-yyyy". I am trying to get the dd part of the string saved into a integer day variable. I want to do the same for the MM and yyyy parts of the String. I can imagine parsing the String using a for loop to separate the String whenever the "/" char is at the current index in the for loop.

How do I split the String containing the date into day, month and year respectively, into integer variables for me to operate on? Perhaps transferring this String to a Date object would make it easier due to a member method or field that handles this (just a thought).

Tom Finet
  • 2,056
  • 6
  • 30
  • 54
  • I don't think it's a duplicate of that one, maybe a different question though. – Sean Wang Aug 13 '16 at 15:30
  • Duplicate: [Java: Get month Integer from Date](http://stackoverflow.com/q/7182996/642706) and [I want to get Year,Month, Day etc from Java Date…](http://stackoverflow.com/q/9474121/642706) and [java date issue in getting month and year](http://stackoverflow.com/q/13602552/642706) and more. – Basil Bourque Aug 13 '16 at 20:10

4 Answers4

4
        String date = "13-08-2016";
        String[] values = date.split("-");
        int day = Integer.parseInt(values[0]);
        int month = Integer.parseInt(values[1]);
        int year = Integer.parseInt(values[2]);

You can use String.split(String regex) method to parse the date to array of String then convert each value to int.

JavaDoc:

public String[] split(String regex) Splits this string around matches of the given regular expression. This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

Read more here: String.split

3

Well please specify the language you are using ..

Basically every language provide functions to parse String to Date and then get day, month and year.

In JS,

var d = new Date("dd-MM-yyyy")
d.getDay();
d.getMonth();
d.getYear();

Similarly in Java,

DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);
Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
1

You'll find the String.split(String regex) method useful for your case.

So in your case you could take the string split it and use the split parts:

String str = "20-04-2016";
String[] splitArray = str.split("-");
int day = Integer.parseInt(splitArray[0]);
int month = Integer.parseInt(splitArray[1]);
int year = Integer.parseInt(splitArray[2]);
Sean Wang
  • 778
  • 5
  • 14
1

You can use

    String dateStr = "14-08-2016"; 
    DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); 
    Date date = (Date)formatter.parse(dateStr);
    System.out.println(date.getMonth()+1);

With Java 8

    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    LocalDate parsedDate = LocalDate.parse(dateStr, formatter1);
    System.out.println(parsedDate.getMonth().getValue());
ravthiru
  • 8,878
  • 2
  • 43
  • 52