将6位数字分成3个部分?

Jav*_*ram 0 c# split

我希望用户得到一个六位数的数字并将其分为3个部分(日,月,年)

例:

int date=111213;
day =11;
month =12;
year =13;
Run Code Online (Sandbox Code Playgroud)

我想我必须将它转换为字符串然后通过使用substring()我可以做到这一点.

任何简单的想法?

Jon*_*eet 6

怎么样:

// Assuming a more sensible format, where the logically most significant part
// is the most significant part of the number too. That would allow sorting by
// integer value to be equivalent to sorting chronologically.
int day = date % 100;
int month = (date / 100) % 100;
int year = date / 10000;

// Assuming the format from the question (not sensible IMO)
int year = date % 100;
int month = (date / 100) % 100;
int day = date / 10000;
Run Code Online (Sandbox Code Playgroud)

(你是否必须以这样的方式存储你的数据?Ick.)

  • 有趣的是,人们如何在不阅读它的情况下赞成Jon Skeet的答案.你有一天和一年错误的方式. (6认同)