0

in the try statement is where i have the problem my imput is a string = "23/03/2003 rossi marco" and my out put needs to be "domenica, 22 marzo 2003" any ideas how i can manage to resolve this problem.

        if(stringaRicevuta == null || stringaRicevuta.equals("FINE")){

            outVersoClient.writeBytes(stringaRicevuta + " (=>server in chiusura...) " + '\n');
            
            System.out.println("Echo sul server in chiusura :" + stringaRicevuta);
            
            break;
        }
        else {
            
            //Divisione dell'input
            String[] parts = stringaRicevuta.split("( )"); //divide dopo ogni spazio

            String toUpperParts1 = parts[1].toUpperCase(); //Rende mauscoli le stringhe
        
            String toUpperParts2 = parts[2].toUpperCase();
            
            String myDateStr=parts[0].toString();
        
            try {
                
                DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ITALIAN);
                
                Date myDate = dateFormat.parse(myDateStr);
                

                outVersoClient.writeBytes( myDate.toString() + " " + "(ricevuta e ritrasmessa)" + " ");
                
                outVersoClient.writeBytes("Nome e Cognome: " + toUpperParts1 + " " + toUpperParts2 + "\n");

                
            } catch (ParseException e) {
                 
                e.printStackTrace();

            }

            System.out.println("6 Echo sul server : " + stringaRicevuta);
        }

    }
Dovelus
  • 11
  • 2
  • 1
    I strongly recommend that you don’t use `DateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 22 '21 at 18:02
  • `LocalDate.parse(string, DateTimeFormatter.ofPattern("dd/MM/uuuu")).format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.ITALIAN))` yields `domenica 23 marzo 2003` on my Java 9. Can you live without the comma? – Ole V.V. Mar 22 '21 at 18:16
  • Actualy, my bad the imput string is 23/03/2003 rossi marco – Dovelus Mar 22 '21 at 19:33
  • In that case you will have to put `parts[0]` instead of `string` in the code in my comment. – Ole V.V. Mar 22 '21 at 19:36
  • ok now if i want call in out put in and place it istead of _myDate_ in _outVersoClient.writeBytes( myDate.toString() + " " + "(ricevuta e ritrasmessa)" + " ");_ – Dovelus Mar 22 '21 at 19:41
  • Si, perche non? I suggest you put the `LocalDate` from `parse()` and the Italian formatter into separate variables, say, `date` and `dateFormatter`. Then `outVersoClient.writeBytes( date.format(dateFormatter) + " " + "(ricevuta e ritrasmessa)" + " ");`. – Ole V.V. Mar 22 '21 at 19:45
  • 1
    like this: LocalDate date = _LocalDate.parse(parts[0], DateTimeFormatter.ofPattern("dd/MM/uuuu"));_ _DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.ITALIAN);_ – Dovelus Mar 22 '21 at 20:19

0 Answers0