0

I want to remove the time from dateAndTime, and also want to have all of them in numeric form like this: 10-10-2019

output: Wed Nov 10 16:39:58 AST 2010

public static void main(String args[]){

    //Java calendar in default timezone and default locale
    int day =10;
    int month =10;
    int year =10;

Calendar m =  Calendar.getInstance();  
 m.set(year, month, day);
  m.add(day, 10);
 System.out.println(" Date :" + m.getTime());
Ahmad Karimi
  • 1,285
  • 2
  • 15
  • 26
sam
  • 11
  • 4
  • 1
    Instead of calendar use Java time api instead – Yoshikage Kira Sep 19 '19 at 03:53
  • 1
    I recommend you don’t use `Calendar` and `Date`. Those classes are 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/). Then use a `DateTimeFormatter` to obtain the output format you want. – Ole V.V. Sep 19 '19 at 04:13
  • When you ask for `10-10-2019`, should that be month-day-year or day-month-year? Also should September 1 be 1-9-2019 or 01-09-2019? You can have it the way you want. – Ole V.V. Sep 19 '19 at 04:23
  • `LocalDate.of(2019, Month.SEPTEMBER, 30).plusDays(10).format(DateTimeFormatter.ofPattern("dd-MM-uuuu"))` gives `10-10-2019`. – Ole V.V. Sep 19 '19 at 04:26

2 Answers2

0

you can use SimpleDateFormat for early versions of java or DateTimeFormatter for java8+.

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

public class DateTests {

    public static void main(String[] args) {
        //java8+
        //recommended
        String formattedToday = DateTimeFormatter.ofPattern("MM-dd-YYYY").format(LocalDateTime.now());
        System.out.println(formattedToday);

        //early versions of java
        Calendar today = Calendar.getInstance();
        String formatted = new SimpleDateFormat("MM-dd-YYYY").format(today.getTime());
        System.out.println(formatted);
    }

}

output:

09-18-2019
09-18-2019

Also read Calendar date to yyyy-MM-dd format in java

prayagupa
  • 30,204
  • 14
  • 155
  • 192
  • 1
    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. Sep 19 '19 at 04:12
0

Use SimpleDateFormat. There's a lot of example you can use.

Erwin
  • 460
  • 2
  • 6
  • 1
    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. Sep 19 '19 at 04:12