0

Converting User Input Date Format from dd/mm/yyyy to yyyymmdd to facilitate date checking. The code writes the date to a .TXT file but write it to it in the dd/mm/yyyy format when I'm wanting it in yyyymmdd. The method ValidDate is what I've tried to fix the problem so far but still isnt working. All help appreciated.


sr864
  • 1
  • 2
  • 1
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – PM 77-1 Jul 31 '19 at 17:08
  • 1
    Please try to format your code for increased readability including proper indentation, removing extra empty lines and removing irrelevant comments. – Joakim Danielson Jul 31 '19 at 17:11
  • FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 31 '19 at 17:27

2 Answers2

1

You need to execute the parse to have a new date from string. Try change your validDate and execute a parse, like this:

    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Date theDate = df.parse(date);
    SimpleDateFormat formater = new SimpleDateFormat ("yyyymmdd");
    String formatedDateAsString = formater.format(theDate);
  • Thanks for replying. Where exactly would I place this code you have given me? just replace what I have in the ValidDate method with this? Sorry quite new to java. Thanks again. – sr864 Jul 31 '19 at 17:36
  • @sr864 to your validateDate function. – Apoorva sahay Jul 31 '19 at 17:58
  • You should create a new method, String dateConverter(String date), for example. Then on dateConverter method you make: SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date theDate = df.parse(date); SimpleDateFormat formatdate = new SimpleDateFormat ("yyyymmdd"); return formatdate.format(theDate); After, you can use this method to write on file: pw.println(dateConverter(formatdate)+","+litres+","+cost); – Mychell Teixeira Jul 31 '19 at 18:04
  • That all works apart from the last line when I amend the pw.println to that netbeans doesn't allow it and it says "Void type not allowed here" and gives me the option to Flip Operands. Am I doing something wrong? Thanks again – sr864 Jul 31 '19 at 19:06
  • dateConverter(..) should returns the formatted string. – Mychell Teixeira Jul 31 '19 at 19:15
  • Still seems to not be working the only error that shows up is for the printwriter line – sr864 Jul 31 '19 at 22:09
  • I've edited the original code at the top to what I have now – sr864 Jul 31 '19 at 22:13
  • Buddy, try do not use variable with initial letter on uppercase. Maybe your IDE is confusing. – Mychell Teixeira Aug 01 '19 at 14:54
0

tl;dr

LocalDate
.parse(
    "23/01/2001" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
)
.format(
    DateTimeFormatter.BASIC_ISO_DATE 
)

See this code run live at IdeOne.com.

20010123

Tip: Better to exchange date-time data textually using only the standard ISO 8601 formats.

java.time

You are using terrible date-time classes that were supplanted years ago by the modern java.time classes as of the adoption of JSR 310.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

ISO 8601

Your desired output format is known as the "Basic" variation of the standard ISO 8601 formats. I suggest considering using the expanded variation, YYYY-MM-DD, as it is more recognizable and readable.

Also, the expanded variations are used by default in the java.time classes when parsing/generating strings. So no need to specify a formatting pattern.

LocalDate ld = LocalDate.parse( "2019-01-23" ) ;  // Parsed by default, no need to specify a formatting pattern. 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154