import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.text.DateFormat;
public class MyClass {
public static void main(String args[]) {
Date scadenza = new Date();
String date = MyClass.dateToString(scadenza, "YYYYMMDD");
System.out.println(date);
}
public static String dateToString(Date date, String format){
if(date == null) return null;
final DateFormat df = new SimpleDateFormat(format);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.format(date);
}
}
I have this piece of code in one of my software, i simplified it rewriting it in an example java class. Today is "2022-04-08" and when i execute this code the variable date is equal to "20220498" but it should be "20220408". How can solve this problem?
>Solution :
Try with "yyyyMMdd" as format param.
D is day in the year according to documentation.
Y is Week year.
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Ps. Ho visto che sei italiano, buon continuo su Stackoverflow!