-2

how do I extract the 12 out of 19921231? Do I use the modulus method or what? Thank you so much. I know how to get the 1992 and the 31, but the tricky part is the 12. Any suggestions?

  • 2
    how do you extract 1992? – Iłya Bursov Feb 16 '15 at 20:53
  • Do you have a fixed format that you can specify? You could just split the string into shorter strings based on number of characters. – chrylis -cautiouslyoptimistic- Feb 16 '15 at 20:53
  • 1
    You could also look at `SimpleDateFormat` (your input looks like a date). – Alexis C. Feb 16 '15 at 20:53
  • Did any of the answers work for you? – Connorelsea Feb 16 '15 at 21:01
  • There are too many ways to answer this question. You should provide more details about the conditions and restrictions you are facing. Ideally, you should provide some code that makes clear the context your are facing the problem in (for example, how you extract `1992`) so that one can give you an answer. – h7r Feb 16 '15 at 21:03
  • System.out.print("enter a date in the YYYYMMDD format:"); int n = userInput.nextInt(); int nDivide = n/10000 ; int n1 = n%10000 ; /*this is last four digits of n*/ int n2 = n1%100; int n3 = n; – Sean Kamela Feb 16 '15 at 21:19
  • Please do not try to input stuff like this as an "integer". Using an integer means that there's some meaning to the number 19 million, 921 thousand, and 231. But in this case there isn't. It's just a `String`, all of whose characters happen to be digits. (I've seen this sort of thing done several times on SO; it's a more serious problem when the string could legitimately have leading 0's, such as for US Zip codes.) – ajb Feb 16 '15 at 22:29
  • possible duplicate of [Fastest way to parse a YYYYMMdd date in Java](http://stackoverflow.com/questions/10013998/fastest-way-to-parse-a-yyyymmdd-date-in-java). Specifically, [this answer](http://stackoverflow.com/a/10014066/642706). Tip: Search for "joda" or "java.time", and "parse". – Basil Bourque Feb 17 '15 at 02:04

5 Answers5

1

If your input string represents a Date then you could do

DateFormat df = new SimpleDateFormat("yyyyMMdd");
Date date = df.parse("19921231");

System.out.println(date.getMonth() + 1);

Or, you can also use the Calendar class as

Calendar cal = Calendar.getInstance();
cal.setTime(df.parse("19921231"));

System.out.println(cal.get(Calendar.DAY_OF_MONTH)); // 31
System.out.println(cal.get(Calendar.YEAR)); // 1992
System.out.println(cal.get(Calendar.MONTH) + 1); // 12
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

You could do a regex match for four characters, two characters, two characters.

Or you could look at the substring method on String.

I would however, given what you appear to be trying to do, recommend you investigate the SimpleDateFormat class

beresfordt
  • 5,088
  • 10
  • 35
  • 43
0

Assuming that's a date format that will always be like that, why not use a subtring() method ( http://www.tutorialspoint.com/java/java_string_substring.htm )?

Or perhaps check this out as well, probably the best option once you do a little research into it: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

MrKespr
  • 31
  • 1
  • 4
0

Use substring.

For example, if you have the following:

    String string = "19921231";

    String year = string.substring(0, 4);
    String month = string.substring(4, 6);
    String day = string.substring(6, 8);

    System.out.println(year);
    System.out.println(month);
    System.out.println(day);

The output is:

1992
12
31

If you would like to convert these back into Integers after getting the smaller parts, you can do something like the following

int year_int = Integer.parseInt(year);
int month_int = Integer.parseInt(month);
int day_int = Integer.parseInt(day);
Connorelsea
  • 2,308
  • 6
  • 26
  • 47
0

you need to use substring:

final int year = Integer.parseInt(str.substring(0, 4));
final int month = Integer.parseInt(str.substring(4, 6));
final int day = Integer.parseInt(str.substring(6));
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57