【问题标题】:How to convert dd/MM/yyyy format date to ccyy/MM/dd format in java? [duplicate]java中如何将dd/MM/yyyy格式日期转换为ccyy/MM/dd格式? [复制]
【发布时间】:2019-01-09 21:40:39
【问题描述】:

我在使用 Java 将 dd/MM/yyyy 格式日期转换为 ccyy/MM/dd 时遇到问题。有人可以帮我吗?如果我能举个例子就好了。

这是我的代码## 示例##

        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); 

提前致谢。

【问题讨论】:

  • 使用您的第一种格式将字符串解析为日期,然后使用第二种格式将其重新格式化为字符串。就这么简单。如果您有任何具体问题,请告诉我们,尤其是向我们展示您在做什么(即您的代码、输入、实际和预期输出)。
  • “我面临一个问题”是什么问题?
  • 还有一个问题:为什么是ccyy 而不是yyyy?除此之外,Jodatime 似乎无法识别小写 c (joda.org/joda-time/apidocs/org/joda/time/format/…)
  • @Thomas 你能告诉我ccyy 是什么吗?在cc 代表什么之前我没听说过?
  • @user12346352 有很多关于如何做的教程,所以问题是“你做了什么研究?” - 如果有一个特定的问题,那就是另一回事了,但在这种情况下,我们需要知道问题是什么。至于ccyy,请参阅我提供的链接。我也不知道,但 Jodatime 似乎为“年度世纪”提供了C——与y 结合似乎没有多大意义。我假设如果我的日期是“21 世纪的第 18 年”,那么我可能会使用 yyCC 而不是 CCyy

标签: java date datetime jodatime


【解决方案1】:

实际上 CCYYMMdd 格式与 yyyyMMdd 格式相同,因为根据 CC(世纪)是年份(整数除以 100) ISO 8601 你可以在post 阅读更多内容

dd/MM/yyyy 日期转换为ccyy/MM/dd 日期很简单

你可以试试这个方法:

    // 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));

输出:

2018/08/02

【讨论】:

  • CCYY真的yyyy吗?据我了解,文档 CC 将代表“一次性”的世纪,即 1996 年(如文档中所示)我会得到 1996 年的 yyyy2096CCyy (因为 1996 年在20 世纪)。
  • 我们需要用 4 位数字格式写年份,所以技术上是 yyyy,而不是 ccyy。如果您出生于 1994 年,那么将 2094 年写成这样的年份是不合逻辑的,因为 1994 年是 20 世纪。有关更多信息,请查看此帖子:stackoverflow.com/a/33420396/5169186
  • 实际上CC 的解释取决于Chronology - 默认是“ISO”而CC 只是year/100。如果使用“公历/朱利安”年表,那么CC 是一回事
  • CC(世纪)也是年份(整数除以 100),因此您可以根据 ISO 8601 考虑 CCYYMMddyyyyMMdd 相同:support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/…
  • 进行一些测试并花时间找到它...见field reference(注意最后 3 列左右 [不仅仅是第一列]!)
【解决方案2】:

这是解决方法

     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);

【讨论】:

  • 这很复杂,不是一个好的实践解决方案。检查我的答案,它简单明了。如果对您有用,请将其标记为已接受。
猜你喜欢
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2012-03-05
  • 1970-01-01
  • 2023-03-15
  • 2017-06-24
  • 1970-01-01
相关资源
最近更新 更多