-4

I am facing an issue while converting dd/MM/yyyy format date to ccyy/MM/dd using Java. Can someone, please help me on this? It would be great If I get some example.

Here is my code## Example##

        SimpleDateFormat dateFormat1 = new SimpleDateFormat("ddMMyyyy");
        Date date1 = new Date();        
        LocalDate date = DateTimeFormat.forPattern("ddMMyyyy").parseLocalDate(dateFormat1.format(date1));
        System.out.println("Century=" + date.getCenturyOfEra()); 
        String usFormat = DateTimeFormat.forPattern("ccyy/MM/dd").print(date);
        System.out.println(usFormat); 

Thanks in advance.

Oghli
  • 2,200
  • 1
  • 15
  • 37
  • 2
    Parse the string to a date with your first format and reformat it to a string with your second. It's as easy as that. If you have any _specific_ problems then tell us and especially show us what you are doing (i.e. your code, input, actual and expected output). – Thomas Aug 02 '18 at 07:44
  • 1
    "I am facing an issue" what is the issue? – Theofanis Aug 02 '18 at 07:45
  • 1
    One additional question: why `ccyy` and not `yyyy`? And besides that, Jodatime doesn't seem to recognize a lowercase `c` (https://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html) – Thomas Aug 02 '18 at 07:47
  • @Thomas can you tell me what `ccyy` is I haven't heard About it before what does the `cc` stand for? – user12346352 Aug 02 '18 at 07:48
  • @user12346352 there are plenty of tutorials on the how so the question would then be "what research did you do?" - If there's a specific issue then that's something else but in that case we need to know what the issue is. As for the `ccyy` see the link I provided. I didn't know that as well but Jodatime seems to provide `C` for "century of the year" - which in combination with `y` doesn't seem to make much sense. I'd assume if I have a date as "the year 18 of the 21st" century then I might use `yy` and `CC` but not `CCyy`. – Thomas Aug 02 '18 at 07:49
  • Hi All, My question was straight forward.If anyone have exact answer to convert dd/MM/yyyy format date To ccyy/MM/dd, Please share.If you are not understanding the question, please let me know.It's not a duplicate one and it's not matched with anything what you shared @jhamon – Venkateswarlu Ala Aug 02 '18 at 08:29
  • Thanks @Thomas , It's because of lowercase `c` .I have changed it to CCYY and it's worked. – Venkateswarlu Ala Aug 02 '18 at 08:40
  • Hi @user12346352, When someone have the issue, you shouldn't say he don't know like that. Actually you are not aware of what's `ccyy`, but I am not worrying about it. – Venkateswarlu Ala Aug 02 '18 at 08:44
  • @VenkateswarluAla I am worrying that you don't know chat `ccyy` as lowercase `c`doesn't exist in [jodatime format](http://www.joda.org/joda-time/key_format.html) – jhamon Aug 02 '18 at 09:01
  • FYI, the Joda-Time project is now in maintenance mode, and advises migration to the *java.time* classes. – Basil Bourque Aug 03 '18 at 03:52

2 Answers2

0

Actually CCYYMMdd format is same as yyyyMMdd format since CC (century) is year (integer divide by 100) according to ISO 8601 you can read more in this post

converting dd/MM/yyyy date to ccyy/MM/dd date is simple

you can try this approach:

    // define date source format 
    SimpleDateFormat sourceformat = new SimpleDateFormat("dd/MM/yyyy");
   // define date target format that you want to convert to it 
    SimpleDateFormat targetformat = new SimpleDateFormat("yyyy/MM/dd");
  // parse the input date as string with source format 
  // and then format it to the required target format 
    String dateAsString = "02/08/2018";
    Date date = sourceformat.parse(dateAsString);
    System.out.println(targetformat.format(date));

Output:

2018/08/02
Oghli
  • 2,200
  • 1
  • 15
  • 37
  • 3
    Is `CCYY` _really_ like `yyyy`? As I understand the documentation `CC` would represent the century which is "one off", i.e. for 1996 (as in the docs) I'd get 1996 for `yyyy` but `2096` for `CCyy` (because 1996 is in the 20th century). – Thomas Aug 02 '18 at 09:08
  • We need to write the year in a 4 digit format, so technically is yyyy,not ccyy. if you born in 1994 it's not logical to write a year like this 2094 since 1994 is in the 20th century. for further information look at this post : https://stackoverflow.com/a/33420396/5169186 – Oghli Aug 02 '18 at 09:17
  • 1
    actually the interpretation of `CC` depends on the `Chronology` - default is "ISO" and `CC` is just `year/100`. If using the "Gregorian/Julian" Chronology, then `CC` is one off – user85421 Aug 02 '18 at 09:53
  • also CC (century) is year (integer divide by 100) so you can consider `CCYYMMdd` is just same as `yyyyMMdd` according to **ISO 8601**: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169814.htm – Oghli Aug 02 '18 at 09:55
  • @CarlosHeuberger you are right. – Oghli Aug 02 '18 at 09:57
  • 1
    toke some testing and time to find it... see [field reference](http://joda-time.sourceforge.net/field.html#CenturyOfEra_and_YearOfCentury) (note the last 3 or so columns [not only the first ones]!) – user85421 Aug 02 '18 at 10:09
-1

Here is the solution for this

     Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    String currentDate = dateFormat.format(date); 
    System.out.println("currentData::"+currentDate);
     DateTime dt = new DateTime(dateFormat.parse(currentDate));
     System.out.println("DT::"+dt);
     DateTimeFormatter fmt = DateTimeFormat.forPattern("CCYY/MM/DD");
     String str = fmt.print(dt);
     System.out.println("CC Date::"+str);
  • It's complicated and not a good practice solution. check my answer it is simple and straightforward. mark it as accepted if it worked for you. – Oghli Aug 04 '18 at 12:33