-6

I want the date to display together with forward slash without any space. If I run the below program, it will only display month's value. Please help.

int month, day, year;
public int displayDate()
{
    int date = month/day/year;
}
System.out.printf("Date: %d%n", d.displayDate());
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 3
    `month/day/year`: Is this not division? – Prashant Apr 04 '18 at 06:14
  • 4
    Numbers don't have slashes. You'll need a string. – shmosel Apr 04 '18 at 06:18
  • 1
    I didn’t downvote the question. In case you were wondering, I suppose the downvotes are because the question seems poorly researched. A not-too-great effort with your search engine would likely at least bring you closer to your goal. On the other hand it’s nice that you have provided [a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), thanks. – Ole V.V. Apr 04 '18 at 08:53

4 Answers4

0

Simplistic Answer

You can use a String to append three int values

e.g.

int month, day, year;
public String displayDate()
{
    return String.format ("%d/%d/%d", month,day, year);
}

System.out.printf("Date: %s%n", d.displayDate());  // change this as well

Of course this code has no bound checking

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

In your code, you are dividing month, day and year with the operator /.

In Java, there is a class SimpleDateFormat to format and display dates.

Try the following code:

public String displayDate(int month, int date, int year) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, date);
    return new SimpleDateFormat("MM/dd/yyyy").format(calendar.getTime());
}

It will return your date in the format MM/dd/yyyy as a String.

Note that month is zero-based.

Check this link out to get some more information about the format: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

M_I
  • 132
  • 5
  • 2
    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. Apr 04 '18 at 08:43
  • You're right, but it depends on the JDK version he is using. `java.time` depends on Java 8. Unfortunately, i am still using `SimpleDateFormat` for my work because i grow up with Java 7. We all should change to `java.time`, yes. Thanks for your comment. – M_I Apr 04 '18 at 09:37
  • 1
    With Java 7 and 6 you may use `LocalDate`, `DateTimeFormatter` and the other `java.time` classes when you add [ThreeTen Backport](http://www.threeten.org/threetenbp/) to your project. And then your code is already futureproof once you move on to Java 8 or later, you will only have to change your import statements (in the backport the classes are in the `org.threeten.bp` package with subpackages). – Ole V.V. Apr 04 '18 at 09:58
  • ThreeTen sounds like third party library. I'm not a friend of those if it is not absolutely necessary. However, i won't teach `SimpleDateFormat` anymore. ;) – M_I Apr 04 '18 at 10:57
  • 1
    The *ThreeTen-Backport* project is led by Stephen Colebourne, the same man who led both the [Joda-Time](http://www.joda.org/joda-time/) project and the [JSR 310](https://www.jcp.org/en/jsr/detail?id=310) java.time project. This is not some flash-in-the-pan weekend-project library. This serious project has been actively developed, tested, and supported. The legacy date-time classes really are a wretched mess, and should be avoided. – Basil Bourque Apr 04 '18 at 15:15
-1

As the documentation clearly states, Character.getNumericValue() returns the character's value as a digit. It returns -1 if the character is not a digit.

If you want to get the numeric Unicode code point of a boxed Character object, you'll need to unbox it first:

-1
import java.text.SimpleDateFormat;

import java.util.Date;

public class DateDemo {

    public static void main(String[] args) {
        DateDemo d = new DateDemo();
        System.out.printf(d.displayDate()); //Output: 04/04/2018
    }

    public String displayDate()
    {
        String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
        return date;
    }
}
Virb
  • 1,639
  • 1
  • 16
  • 25