I do not know what I am doing wrong? But what I am trying to do is format my dateDue String from SharedPreferences to the format of MM/dd/yyyy from MMddyyyy.
But my code below returns all dates with slashes but it displays the date 12/31/1969 and not my SharedPreferences number, which should display 12/30/2022. Why would it go back so far. Does it have to do with something with Calendar?
String myFormat = "MM/dd/yyyy";
SimpleDateFormat format = new SimpleDateFormat(myFormat);
SharedPreferences settings = context.getApplicationContext().getSharedPreferences(name, 0);
Integer dateDue = Integer.valueOf(settings.getInt("dateDue", 0));
format = new SimpleDateFormat(myFormat);
String date = format.format(dateDue);
if (Integer.valueOf(String.valueOf(dueAmount)) > 0) {
holder.getDateDue.setText(String.valueOf(date));
}
CodePudding user response:
Let's walk through what is happening, it's kind of funny if you know Java.
format.format(dateDue)
is implemented by DateFormat.format(Object)
(because SimpleDateFormat extends DateFormat and inherits format(Object)
and does not override it). The code for that is:
if (obj instanceof Date)
return format( (Date)obj, toAppendTo, fieldPosition );
else if (obj instanceof Number)
return format( new Date(((Number)obj)...
obj
in this case is an Integer which is a Number, so it calls format(new Date(obj)...
new Date(<some integer>)
creates a date that is <some integer>
milliseconds from January 1, 1970. https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#Date-long-
And <some integer>
here is 12302022, or 12.3 million milliseconds or 12,300 seconds or 3.4 hours after January 1, 1970 at midnight, and then with the timezone difference you get 12/31/1969.
CodePudding user response:
tl;dr
LocalDate.parse(
"12302022" ,
DateTimeFormatter.ofPattern( "ddMMuuuu" )
)
Details
The Answer by Mike Kim appears to be correct, and quite clever in detecting the problem as your Question is not clearly stated. That Answer determined your input must have been "12302022"
.
java.time
Additionally, you should know that you are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.
To parse your input 12302022
in DDMMYYYY format, define a formatting pattern to match.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "ddMMuuuu" ) ;
LocalDate
Use that to parse the string into a LocalDate
object.
LocalDate ld = LocalDate.parse( "12302022" , f ) ;
ld.toString() = 2022-12-30
ISO 8601
That output of LocalDate.toString
is in standard ISO 8601 format.
Tip: Using ISO 8601 formats for storing and exchanging date-time values textually will make your life much easier. The formats are designed to be easily parsed by machine as well as easily read by humans. The java.time classes use ISO 8601 formats by default when parsing/generating text.
Android
Android 26 comes with an implementation of the java.time classes.
In earlier Android, use the latest tooling to access most of the java.time functionality via “API desugaring”.