将字符串日期转换为字符串日期不同格式 [英] Convert String Date to String date different format

查看:49
本文介绍了将字符串日期转换为字符串日期不同格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手.Postgres db 包含的日期格式是 yyyy-MM-dd.我需要转换为dd-MM-yyyy.

I am new to Java. Postgres db contain date format is yyyy-MM-dd. I need to convert to dd-MM-yyyy.

我已经试过了,但是显示错误的结果

I have tried this, but wrong result is display

   public static void main(String[] args) throws ParseException {

    String strDate = "2013-02-21";
      DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      Date da = (Date)formatter.parse(strDate);
      System.out.println("==Date is ==" + da);
      String strDateTime = formatter.format(da);

      System.out.println("==String date is : " + strDateTime);
}

推荐答案

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("2013-02-21");
System.out.println(format2.format(date));

这篇关于将字符串日期转换为字符串日期不同格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆