Convert calendar date to string?

I am trying to get the program to call the current date, add 30 days to it, and then put this date as a string.

// Set calendar for due date on invoice gui Calendar cal = Calendar.getInstance(); // Add 30 days to the calendar for the due date cal.add(Calendar.DATE, 30); Date dueDate = cal.getTime(); dueDatestr = Calendar.toString(dueDate); 
+1
source share
4 answers

And the question?

If you want to format the date, I suggest java.text.SimpleDateFormat instead of toString() . You can do something like:

 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); dueDateStr = dateFormat.format(dueDate); // renders as 11/29/2009 
+3
source

You have almost none:

 Date dueDate = cal.getTime(); String dueDateAsString = dueDate.toString(); 

or

 String dueDateAsFormattedString = DateFormat.format(dueDate); 
+1
source

You might want to use FastDateFormat from the Apache community instead of SimpleDateFormat, because SimpleDateFormat is not thread safe.

 FastDateFormat dateFormat = FastDateFormat.getInstance("MM/dd/yyyy"); dueDateStr = dateFormat.format(dueDate); 

This is especially true if you want to use a static instance of date formatting, which is a common temptation.

0
source

You can do it easily with my class: https://github.com/knyttl/Maite/wiki/Maite-Date-and-Time

 new Time() .plus(1, Time.DAY) .format("yyyy-MM-dd"); 
0
source

Source: https://habr.com/ru/post/896694/


All Articles