0

I use the following code to add one day to the calendar. However, I want to retrieve in a string only the date without the time. Is this somehow possible?

  Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,1);
String dateandtime=calendar.getTime();

Update: Thanks for your suggestions. The similar posts suggested is too complex for a newbie like me in java. The answer provided in this question is simple. That is why I suggest this question should not be closed.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
stefanosn
  • 3,264
  • 10
  • 53
  • 79
  • https://stackoverflow.com/questions/5050170/how-do-i-get-a-date-without-time-in-java – Thunderbolt Engineer Oct 19 '20 at 13:20
  • You can use calendar.get(DATE), calendar.get(MONTH) and calendar.get(YEAR) for day , month and year. – Jawad El Fou Oct 19 '20 at 13:21
  • Thanks for your suggestion i will try it. – stefanosn Oct 19 '20 at 13:41
  • No its a different case. – stefanosn Oct 19 '20 at 15:18
  • I recommend you don’t use `Calendar`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). A `LocalDate` is a date without time of day, so exactly what you need. – Ole V.V. Oct 19 '20 at 16:38
  • How is that other case different? Just so we don’t repeat the same answers, which would be useless for you. – Ole V.V. Oct 19 '20 at 16:46
  • Update: Thanks for your suggestions. The similar posts suggested is too complex for a newbie like me in java. The answer provided in this question is simple. That is why i suggest this question should not be closed. – stefanosn Oct 20 '20 at 01:31

2 Answers2

3

This might help

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");

String formatted = format1.format(cal.getTime());
System.out.println(formatted);
// Output "2020-10-19"
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Oct 19 '20 at 16:37
0

java.time

For a simple, reliable and up-to-date solution I recommend that you use java.time, the modern Java date and time API, for your date work.

    LocalDate today = LocalDate.now(ZoneId.of("Europe/Athens"));
    LocalDate tomorrow = today.plusDays(1);
    System.out.println(tomorrow);

When I ran this snippet just now (19 October), the output was:

2020-10-20

A LocalDate is a date without time of day (and without time zone), so it seems to me that this gives you exactly what you need, no more, no less.

For a string you may use tomorrow.toString() or use a DateTimeFormatter. Search for how to do the latter, it’s described in many places.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161