2-digit-year

Error converting date with two digits for the Year field

岁酱吖の 提交于 2021-02-08 06:55:31
问题 // input format: dd/MM/yy SimpleDateFormat parser = new SimpleDateFormat("dd/MM/yy"); // output format: yyyy-MM-dd SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(formatter.format(parser.parse("12/1/20"))); // 0020-11-01 I am using the above code but it is giving me year as '0020' instead of '2020'. 回答1: Use java.time for this: public static void main(String[] args) { String dateString = "12/1/20"; LocalDate localDate = LocalDate.parse(dateString,

Joda Time & parsing two digit year correctly based on pivot

↘锁芯ラ 提交于 2020-01-24 05:48:04
问题 I have the following (somewhat abbreviated) code to parse String input into a Joda Time DateTime object. I need to properly handle multiple formats, including four & two digit years. setupValidDateFormats(); DateTime date = convertToDateTime(thisField.getText()); if (date != null) { thisField.setText(date.toString("MM/dd/yyyy")); } else { System.out.println("Invalid date"); } private void setupValidDateFormats() { DateTimeParser[] formats = { DateTimeFormat.forPattern("MM/dd/yyyy").getParser(

Localize string from `MonthDay` or `YearMonth` classes in java.time? [duplicate]

非 Y 不嫁゛ 提交于 2020-01-03 02:52:32
问题 This question already has answers here : How to format YearMonth and MonthDay depending on a Locale? (3 answers) Closed 2 years ago . The java.time classes built into Java 8 and later offer the MonthDay and YearMonth classes. Their toString and parse methods use standard ISO 8601 formats ( --MM-DD & YYYY-MM ), which is wise. For presentation to humans, the standard formats may not be suitable. Is there anyway to generate an automatically localized string to represent the values in objects of

Localize string from `MonthDay` or `YearMonth` classes in java.time? [duplicate]

别说谁变了你拦得住时间么 提交于 2020-01-03 02:52:00
问题 This question already has answers here : How to format YearMonth and MonthDay depending on a Locale? (3 answers) Closed 2 years ago . The java.time classes built into Java 8 and later offer the MonthDay and YearMonth classes. Their toString and parse methods use standard ISO 8601 formats ( --MM-DD & YYYY-MM ), which is wise. For presentation to humans, the standard formats may not be suitable. Is there anyway to generate an automatically localized string to represent the values in objects of

Parsing string to local date doesn't use desired century

丶灬走出姿态 提交于 2019-12-17 05:12:32
问题 I am using this DateTimeFormatter: DateTimeFormatter.ofPattern("ddMMYY") I want to parse the string 150790 and I got this error: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed Obviously, I want to get the following TemporalAccessor : {DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990} Do you know why I got the year 2090 instead of 1990? Thanks for your help 回答1: Since this question

Parsing string to local date doesn't use desired century

走远了吗. 提交于 2019-12-17 05:12:06
问题 I am using this DateTimeFormatter: DateTimeFormatter.ofPattern("ddMMYY") I want to parse the string 150790 and I got this error: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed Obviously, I want to get the following TemporalAccessor : {DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990} Do you know why I got the year 2090 instead of 1990? Thanks for your help 回答1: Since this question

2 digit years using strptime() is not able to parse birthdays very well

房东的猫 提交于 2019-12-04 03:17:05
问题 Consider the following birthdays (as dob ): 1-Jun-68 1-Jun-69 When parsed with Python’s datetime.strptime(dob, '%d-%b-%y') will yield: datetime.datetime(2068, 6, 1, 0, 0) datetime.datetime(1969, 6, 1, 0, 0) Well of course they’re supposed to be born in the same decade but now it’s not even in the same century! According to the docs this is perfectly valid behaviour: When 2-digit years are accepted, they are converted according to the POSIX or X/Open standard: values 69-99 are mapped to 1969

How to convert two digit year to full year using Java 8 time API

岁酱吖の 提交于 2019-12-04 03:01:21
问题 I wish to remove the Joda-Time library from my project. I am trying to convert a two digit year to full year. The following code from Joda-Time can fulfil the purpose. Below is the following code of joda-time DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormat.forPattern("yy"); int year = LocalDate.parse("99"", TWO_YEAR_FORMATTER).getYear(); System.out.println(year); Output: 1999 This is the output that I expect and that makes sense in my situation. However, when I try the same procedure

How to convert two digit year to full year using Java 8 time API

烂漫一生 提交于 2019-12-01 15:59:28
I wish to remove the Joda-Time library from my project. I am trying to convert a two digit year to full year. The following code from Joda-Time can fulfil the purpose. Below is the following code of joda-time DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormat.forPattern("yy"); int year = LocalDate.parse("99"", TWO_YEAR_FORMATTER).getYear(); System.out.println(year); Output: 1999 This is the output that I expect and that makes sense in my situation. However, when I try the same procedure with java.time API, it produces a DatetimeParseException. Below is the following code of java.time API:

Converting a year from 4 digit to 2 digit and back again in C#

六眼飞鱼酱① 提交于 2019-11-30 02:38:25
My credit card processor requires I send a two-digit year from the credit card expiration date. Here is how I am currently processing: I put a DropDownList of the 4-digit year on the page. I validate the expiration date in a DateTime field to be sure that the expiration date being passed to the CC processor isn't expired. I send a two-digit year to the CC processor (as required). I do this via a substring of the value from the year DDL. Is there a method out there to convert a four-digit year to a two-digit year. I am not seeing anything on the DateTime object. Or should I just keep processing