-1
newDate =(newDaysAdded+"/"+month+"/"+year); 
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
try {
  Date newDateOne = date.parse(newDate);

When I try:

view.writeToScreen(newDateOne);

I get the error:

 Incompatible types: Date cannot be converted to String.

Here is the writeToScreen method:

void writeToScreen(String s);

However from there I can't turn into a string, I have tried: String test = Date.toString(newDateOne);

Can anyone help?

Jake Wells
  • 15
  • 5

3 Answers3

0

Based on your existing code you could use:

writeToScreen (date.format(newDateOne));
Chris Peacock
  • 3,011
  • 1
  • 21
  • 20
  • however now when I print it to the screen I get the full "Sat Nov 01 00:00:00 GMT 1997" where as I'd like it to be 01/11/1997. Is there a way I can just access those parts specifically and just print them in that format? – Jake Wells Dec 15 '16 at 17:40
  • Hmmm.... I think I'd need to see the whole code, as that ought to work if placed in the code you posted. – Chris Peacock Dec 15 '16 at 18:04
0

Change

view.writeToScreen(newDateOne);

to

view.writeToScreen(newDateOne.toString());
Naman
  • 23,555
  • 22
  • 173
  • 290
  • Thankyou this worked however now when I print it to the screen I get the full "Sat Nov 01 00:00:00 GMT 1997" where as I'd like it to be 01/11/1997. Is there a way I can just access those parts specifically and just print them in that format? – Jake Wells Dec 15 '16 at 17:40
  • @JakeWells whats your input `newDate =(newDaysAdded+"/"+month+"/"+year);` for this line? and what does `writeToScreen` does? – Naman Dec 15 '16 at 18:47
0

Try to look at all the methods... Either in the API doc or even in the intellisense in a code editor

    public static void main(String[] args) {
        String newDaysAdded = "05";
        String month = "04"; 
        String year = "2014";
        String newDateString = (newDaysAdded+"/"+month+"/"+year);

        SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy");
        try {
            Date newDate = SDF.parse(newDateString); //Takes string and convert it to date

            //If i understand the question you dont just want to print it as a date...
            System.out.println(newDate);   //Sat Apr 05 00:00:00 CAT 2014

            //You want to print it as a string inf the format you specified in the SDT
            System.out.println(SDF.format(newDate)); //Print  as in05/04/2014
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
Chrispie
  • 1,026
  • 4
  • 24
  • 50
  • I would like it in the format "dd/MM/yyyy" however when I tried to do date.format(newDate) I got the error: Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Cannot format given Object as a Date – Jake Wells Dec 15 '16 at 17:50
  • What is your code looking like? Helps if you paste the full section – Chrispie Dec 15 '16 at 18:12