Convert a two digit year to a four digit year

前端 未结 17 888
误落风尘
误落风尘 2020-12-25 12:13

This is a question of best practices. I have a utility that takes in a two digit year as a string and I need to convert it to a four digit year as a string. right now I do <

17条回答
  •  被撕碎了的回忆
    2020-12-25 12:43

    I think Java has a good implementation of this:

    http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#year

    People rarely specify years far into the future using a two-digit code. The Java implementation handles this by assuming a range of 80 years behind and 20 years ahead of the current year. So right now, 30 would be 2030, while 31 would be 1931. Additionally, this implementation is flexible, modifying its ranges as time goes on, so that you don't have to change the code every decade or so.

    I just tested, and Excel also uses these same rules for 2-digit year conversion. 1/1/29 turns into 1/1/2029. 1/1/30 turns into 1/1/1930.

提交回复
热议问题