-1

I am building a text localisation library using only packages beginning with Java. or Javax. ("Standard Java API's").

The ultimate goal of the library is to read a text file, identify and change the dates and currencies from the sourceFormat (the format from the text file) which will be declared by the user for ease of formatting, to the outputFormat (again declared by the user).

The outputFormats are then inserted into a new string in the correct positions and then saved to a new text file.

The only piece of "the puzzle" left to solve is for formatting/localising the dates.

I currently have various methods coded that will extract these 3 variables (instantiated below with potential examples):

String outputFormat = "dd/MM/yyyy";   // UK Format

String sourceFormat = "yyyy-MM-dd";  // German Format

String dateFromText = "26/05/2001";

After reading around I cannot find any way to do this for the dates with ease in Java.... (Maybe I'm missing something??)

So my question is what would be the easiest way to code this? (By easiest I mean this quickest for me to implement and get the code working. Not worried at this point about the efficiency of the code).

I found this code on SO for a similar problem in C#: How to convert any date format to yyyy-MM-dd

string DateString = "11/12/2009";
IFormatProvider culture = new CultureInfo("en-US", true); 
DateTime dateVal = DateTime.ParseExact(DateString, "yyyy-MM-dd", culture);

Something like this but for Java would be perfect.

Thanks!

  • 1
    Does this answer your question? [How do I convert the date from one format to another date object in another format without using any deprecated classes?](https://stackoverflow.com/questions/12503527/how-do-i-convert-the-date-from-one-format-to-another-date-object-in-another-form) – Savior May 26 '20 at 14:27
  • I guess your major problem will be to automatically detect the correct format. Conversion itself is easily done (as @Savior already pointed out). But let's say you read this: `08/05/2020` without knowing the format. It could be may 8th or August the 5th. – GameDroids May 26 '20 at 14:31
  • @Savior - Yeah I think so, I will go and try to implement this within my code. Thanks – BigBirdGrim May 26 '20 at 14:38
  • 1
    @GameDroids - Yes that's also part of the problem, although because the user can specify the ```sourceFormat```, I can use a map with a switch case to extract a format. For instance user types UK, ```Switch(UK)``` ```case UK:```, ```map.get UK```, returns key ```"dd/MM/yyyy"``` – BigBirdGrim May 26 '20 at 14:39
  • Tip: When exchanging date-time values as text, use only ISO 8601 standard formats. Other formats should only be generated for localized presentation to the user, not for data exchange. – Basil Bourque May 26 '20 at 15:14
  • To automatically localize, use the `DateTimeFormatter.ofLocalized…` methods. – Basil Bourque May 26 '20 at 15:16
  • 1
    Thanks for all your answers, @savior that post did help so thanks! – BigBirdGrim May 26 '20 at 15:39

1 Answers1

-1

your variables are in string change them to Date or LocalDate. Test here to see.

private LocalDate dateloc;

createObj = new nameClase();
Date.valueOf(createObj.getDateNaiss().toString());
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Diouf AH
  • 1
  • 1