更改日期 Java 的格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18628059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:39:09  来源:igfitidea点击:

Change the format of Date Java

javadate

提问by Cristian Chaparro A.

I've the Date object with the format

我有格式的 Date 对象

 /107/2013 12:00:00 AM

Expected value for me:

对我的期望值:

2013-07-01

How I do this?.

我如何做到这一点?

I'm trying with this code

我正在尝试使用此代码

public static  Date  formatearFecha( Date fecha ){

    String fechaString = new SimpleDateFormat("yyyy-MM-dd").format(fecha) ;
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date fechaFormateada = null;
    try {
        fechaFormateada = df.parse(fechaString);
    } catch (ParseException ex) {
        Logger.getLogger(TestThings.class.getName()).log(Level.SEVERE, null, ex);
    }
    return fechaFormateada;

}

When I try to make a test I get this:

当我尝试进行测试时,我得到了这个:

 System.out.println( formatearFecha(myDate) );
 Mon Sep 30 00:00:00 COT 2013

Update:

更新:

my problem was in sql change to java.util.Date to java.sql.Date
I solve this so:

我的问题是将 sql 更改为 java.util.Date 到 java.sql.Date
我解决了这个问题:

private  String formatearFecha( Date fecha ){
    return new SimpleDateFormat("yyyy-MM-dd").format(fecha);
}

private java.sql.Date stringToSQLDate( String fecString ){

    java.sql.Date fecFormatoDate = null;
    try {
          SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd", new Locale("es", "ES"));
         return  new java.sql.Date(sdf.parse(fecString).getTime());
    } catch (ParseException ex) { }
    return null;
}

And test:

并测试:

stringToSQLDate( formatearFecha( myDate ) );

回答by gerrytan

First of all: java.util.Date object neverhas a format stored in it. You can think of Date as an object containing long value of milliseconds since epoch at UTC timezone. The class has few methods, but no timezone offset, formatted pattern etc stored in it.

首先:java.util.Date 对象从来没有存储在其中的格式。您可以将 Date 视为包含自 UTC 时区纪元以来的长毫秒值的对象。该类的方法很少,但没有存储在其中的时区偏移、格式化模式等。

Secondly, following basic code can be used to format a date to yyyy-MM-dd in your machine's locale:

其次,以下基本代码可用于在您的机器的语言环境中将日期格式化为 yyyy-MM-dd:

Date mydate = ...
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String mydateStr = df.format(mydate);

Similarly, you can parse a string "/107/2013 12:00:00 AM" into a date object in your machine's locale like this:

类似地,您可以将字符串“/107/2013 12:00:00 AM”解析为机器语言环境中的日期对象,如下所示:

String mydateStr = "/107/2013 12:00:00 AM";
DateFormat df = new SimpleDateFormat("/dMM/yyyy HH:mm:ss aa");
Date mydate = df.parse(mydateStr);

Two method above can be used to change a formatted date string from one into the other.

上述两种方法可用于将格式化的日期字符串从一种更改为另一种。

See the javadoc for SimpleDateFormatfor more info about formatting codes.

有关格式化代码的更多信息,请参阅 SimpleDateFormat 的 javadoc

回答by AJJ

Date myDate = .......
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormat.format(myDate));