【问题标题】:Date Format from Date in String to Date [duplicate]从字符串中的日期到日期的日期格式[重复]
【发布时间】:2011-08-27 00:35:21
【问题描述】:

可能重复:
String to Date in Different Format in Java

嗨,

我接收的日期为 MM/dd/yyyy 格式的 String 数据类型,需要转换为 yyyy-MM-dd 格式的数据 Datatype 以存储在 DB 中。

【问题讨论】:

标签: java


【解决方案1】:

您可以使用 SimpleDateFormat:

SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat destinationFormat = new SimpleDateFormat("yyyy-MM-dd");

String result = sourceFormat.format(destinationFormat.parse(input));

【讨论】:

    【解决方案2】:

    使用格式化程序将日期解析为特定类型

     String date="07/25/2011";
        SimpleDateFormat dateFormatter=new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date d=dateFormatter.parse(date);
    
                } catch (ParseException e) {
                      e.printStackTrace();
                }
    

    【讨论】:

    • 呃,试试看。检查 ParseException 是一个非常愚蠢的想法。现在我们必须忍受它:(
    【解决方案3】:

    你也可以这样做

    String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
    
    //Instance of the calender class in the utill package
    Calendar cal = Calendar.getInstance(); 
    
    //A class that was used to get the date time stamp
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); 
    

    打印出你说的时间

    System.out.println(sdf.format(cal.getTime()) );
    

    干杯

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多