Java 是否有一种简单的方法将日期(sql)转换为以下格式月(3个字符)日(int)、年(2014)

Java 是否有一种简单的方法将日期(sql)转换为以下格式月(3个字符)日(int)、年(2014),java,javascript,sql,postgresql,Java,Javascript,Sql,Postgresql,是否有一种简单的方法将日期(sql)转换为以下格式月(3个字符)日(int)、年(int) 例如: 2014年1月3日 2014年2月2日 我有这样一句话:“2014-02-14” (我在客户端使用postgresql、java和javascript)看看java的API 你可以这样做-- 这将有助于: SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy"); SimpleDateFormat sqlform

是否有一种简单的方法将日期(sql)转换为以下格式月(3个字符)日(int)、年(int)

例如: 2014年1月3日 2014年2月2日 我有这样一句话:“2014-02-14”

(我在客户端使用postgresql、java和javascript)

看看java的API

你可以这样做--

这将有助于:

    SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");

    SimpleDateFormat sqlformat = new SimpleDateFormat("yyyy-mm-dd");

    java.util.Date date;
    try {
        date = sqlformat.parse("2014-02-14");
        String result = format.format(date);
        System.out.println(result);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // Pass your SQL date object here

假设您希望在数据库端本身实现这一点。然后使用下面的sql查询

假设“stack”是包含日期值的列,即“2014-02-14”

托查 -- 2014年2月14日 简短回答 在Joda Time 2.3中(替换所需的时区和区域设置)

日期时间对象没有格式 将日期时间值与其字符串表示形式混为一谈是一个常见的错误java.sql.Date对象没有字符串格式。可以从日期时间对象生成字符串表示,但这两个对象是独立的

ISO 8601 您提到的格式由标准定义。日期时间库使用ISO 8601作为输入和输出的默认值

时区 这个问题和其他答案都忽略了时区这一关键问题

决定哪一天(昨天、今天、明天)取决于你所在的时区。2月2日,巴黎迎来了新的一天,而在蒙特勒尔,这一天仍然是2月1日

实例没有时区。它的日期时间值在内部有效为(无偏移)

体面日期时间库 java中捆绑的java.util.Date和.Calendar类是出了名的麻烦。避开它们。使用以下任一选项:

  • 新版本使用Java8
乔达时间 Joda Time2.3中的一些示例代码

获取当前日期和时间

DateTimeZone timeZoneParis = DateTimeZone.forID( "Europe/Paris" );
DateTime nowInParis = DateTime.now( timeZone );
要将java.sql.Date实例转换为Joda时间对象,只需将其传递给构造函数。确保包含要分配给Joda time对象的时区。如果省略时区,则会分配JVM的默认区域

DateTime dateTimeInParis = new DateTime( myJavaDotSqlDotDateObject, timeZoneParis );
调整时区

DateTimeZone timeZoneMontréal = DateTimeZone.forID( "America/Montreal" );
DateTime nowInMontréal = nowInParis.withTimeZone( timeZone );
您可以调整到UTC。这可能有助于调试,以验证存储在数据库中的UTC值始终以UTC格式存储
时间戳
类型
。我重复:带时区的
时间戳
和不带时区的
时间戳
类型都存储在UTC中。“with”或“without”名称具有误导性,因为它们指的不是存储,而是在插入数据库或从数据库中检索时是否遵守时区:始终将时间戳与时区一起使用

DateTime dateTimeInUtc = nowInMontréal.withTimeZone( DateTimeZone.UTC );
使用内置的ISO 8601格式化程序之一生成日期时间值的字符串表示形式。对于您提到的仅日期
YYYY-MM-DD
格式,请调用工厂方法

对于所需的
mmmdd,YYYY
格式,我建议您改用本地化格式。有关示例,请参见此答案顶部的代码行。传递“M-”以生成日期部分的中等长度字符串表示,同时省略时间部分。通过“F”、“L”、“M”或“S”表示完整、长、中、短

如果坚持使用指定的格式,可以使用
DateTimeFormat.forPattern
方法创建格式化程序。在StackOverflow中搜索许多示例

请注意,格式化程序可以作为其过程的一部分进行时区调整,作为我们上面看到的时区调整的替代方法。格式化程序还可以本地化

DateTimeFormat formatter = ISODateTimeFormat.date().withTimeZone( timeZoneMontréal ).withLocale( new java.util.Locale( "fr", "CA" ); // Factory producing formatters.
String outputDateOnlyInMontréal = formatter.print( nowInParis );

谢谢,但是现在它总是一个月一个月地回来
DateTime dateTimeInParis = new DateTime( myJavaDotSqlDotDateObject, timeZoneParis );
DateTimeZone timeZoneMontréal = DateTimeZone.forID( "America/Montreal" );
DateTime nowInMontréal = nowInParis.withTimeZone( timeZone );
DateTime dateTimeInUtc = nowInMontréal.withTimeZone( DateTimeZone.UTC );
DateTimeFormat formatter = ISODateTimeFormat.date().withTimeZone( timeZoneMontréal ).withLocale( new java.util.Locale( "fr", "CA" ); // Factory producing formatters.
String outputDateOnlyInMontréal = formatter.print( nowInParis );