标签: simpledateformat

对于自定义时区,没有Java(GMT + 00:00)的时区偏移的SimpleDateFormat

是否可以使用SimpleDateFormat类格式化Java中的日期时间,以使日期的时区部分在其后面没有+0000.

编辑

我们正在更改Java中的默认时区,如下所示:

SimpleTimeZone tz = new SimpleTimeZone(0, "Out Timezone");        
TimeZone.setDefault(tz);
Run Code Online (Sandbox Code Playgroud)

不幸的是,我无法删除此代码.我会猜测整个系统停止工作.我认为最初的作者把这个用于解决某些节能问题.

考虑到这一点,我想将日期格式化为:

2011-12-27 09:00 GMT

要么

2011-12-27 09:00 BST

我只能将SimpleDateFormat输出为:

2011-12-27 09:00:00 GMT + 00:00

它使用格式字符串yyyy-MM-dd HH:mm:ss z

我看不到简单时区对冬令时(GMT)id或夏令时id(BST)有任何参考的地方.

有什么想法吗?

谢谢

Andez

java timezone simpledateformat

12
推荐指数
2
解决办法
4万
查看次数

为什么非宽松的SimpleDateFormat用字母解析日期?

当我运行以下代码时,我会期待一个堆栈跟踪,但它似乎忽略了我的的错误部分,为什么会发生这种情况?

package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
  public static void main(final String[] args) {
    final String format = "dd-MM-yyyy";
    final String value = "07-02-201f";
    Date date = null;
    final SimpleDateFormat df = new SimpleDateFormat(format);
    try {
      df.setLenient(false);
      date = df.parse(value.toString());
    } catch (final ParseException e) {
      e.printStackTrace();
    }
    System.out.println(df.format(date));
  }

}
Run Code Online (Sandbox Code Playgroud)

输出是:

07-02-0201

java simpledateformat

12
推荐指数
2
解决办法
2268
查看次数

将String Date转换为String date不同的格式

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

我试过这个,但显示错误的结果

   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);
}
Run Code Online (Sandbox Code Playgroud)

java date-conversion simpledateformat

12
推荐指数
3
解决办法
4万
查看次数

解析"YYYY-MM-dd HH:mm"时,SimpleDateFormat产生错误的日​​期时间

我试图解析一个String(YYYY-MM-dd HH:mm)Date,但是错误的日期比预期的错.

码:

Date newDate = null;
String dateTime = "2013-03-18 08:30";
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm", Locale.ENGLISH);
df.setLenient(false);
try {
    newDate = df.parse(dateTime);
} catch (ParseException e) {
    throw new InvalidInputException("Invalid date input.");
}
Run Code Online (Sandbox Code Playgroud)

生产:

美国东部时间2012年12月30日08:30(错误)

我试过设置Lenient但没有运气.

更新

感谢Sudhanshu的回答,它帮助我解决了Java转换问题.当我从上面的代码输入返回的日期到数据库时,我正确地得到了日期,但时间总是如此00:00.

ps.setDate(3, new java.sql.Date(app.getDate().getTime()));
Run Code Online (Sandbox Code Playgroud)

java datetime simpledateformat datetime-parsing

12
推荐指数
3
解决办法
4万
查看次数

OutOfMemoryException(升级到JDK 7u45之后)

升级到最新的JDK后,我们(在某些机器上)有一个奇怪的OutOfMemoryException.

考虑这个简单的应用:

public class Test
{
    public static void main (String[] args) {
        try {
            java.text.SimpleDateFormat dateFormatter = new java.text.SimpleDateFormat("E dd/MM/yyyy HH:mm");
            System.out.println("formatted date: " + dateFormatter.format(new java.util.Date()));
        } catch (Exception x) {
            System.err.println(x);
            System.exit(1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

运行这个小程序将导致此异常(即使在运行时-Xmx2048M -Xms2048):

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Currency.readLongArray(Currency.java:657)
    at java.util.Currency.access$100(Currency.java:76)
    at java.util.Currency$1.run(Currency.java:211)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.util.Currency.<clinit>(Currency.java:192)
    at java.text.DecimalFormatSymbols.initialize(DecimalFormatSymbols.java:566)
    at java.text.DecimalFormatSymbols.<init>(DecimalFormatSymbols.java:94)
    at java.text.DecimalFormatSymbols.getInstance(DecimalFormatSymbols.java:157)
    at java.text.NumberFormat.getInstance(NumberFormat.java:767)
    at java.text.NumberFormat.getIntegerInstance(NumberFormat.java:439)
    at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:664)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:585)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:560)
    at …
Run Code Online (Sandbox Code Playgroud)

java out-of-memory simpledateformat java-7

12
推荐指数
1
解决办法
1079
查看次数

将SimpleDateFormat转换为DateTimeFormatter

因此,当尝试使用SimpleDateFormat和Date替换一些遗留代码时,要使用java.time.DateTimeFormatter和LocalDate,我遇到了一个问题.两种日期格式不相同.在这一点上,我必须说我知道两种日期类型不一样,但我所处的场景意味着我从不关心时间方面,所以可以忽略它.

public Date getDate(String value) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    try {
        return dateFormat.parse(value);
    } catch (ParseException e) {
        return null;
    }
}

public LocalDate getLocalDate(String value) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    try {
        return LocalDate.parse(value, formatter);
    } catch (DateTimeParseException e) {
        return null;
    }
}

public void testDates() {
    getDate("03/07/2016");               // Sun Jul 03 00:00:00 BST 2016
    getDate("3/7/2016");                 // Sun Jul 03 00:00:00 BST 2016
    getDate("3/7/2016 00:00:00");        // Sun Jul 03 00:00:00 BST 2016
    getDate("3/7/2016 00:00:00.0+0100"); // Sun …
Run Code Online (Sandbox Code Playgroud)

java datetime-format simpledateformat java-8

12
推荐指数
1
解决办法
9253
查看次数

为什么Java的SimpleDateFormat会解析这个问题

嗨,我有一个简单的日期格式设置自定义格式字符串:MMddyy

我给它以下值来解析:4 1 01

我不认为它应该解析这个因为空格,但简单日期格式返回日期

4月4日0001AD

任何想法为什么?

java date-format simpledateformat

11
推荐指数
1
解决办法
9554
查看次数

日期格式JAVA

我想格式化2012-05-04 00:00:00.004-MAY-2012.我已经尝试过以下步骤.

    SimpleDateFormat sdf = new SimpleDateFormat(
            "yyyy-MM-dd 'T' HH:mm:ss.SSS");

    Date date;
    String dateformat = "";
    try {
        date = sdf.parse("2012-05-04 00:00:00.0");
        sdf.applyPattern("DD-MON-RR");
        dateformat = sdf.format(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

但我得到了以下异常.

java.text.ParseException: Unparseable date: "2012-05-04 00:00:00.0"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at com.am.test.Commit.main(Example.java:33)`
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?

java datetime simpledateformat

11
推荐指数
1
解决办法
4万
查看次数

解析字符串到目前为止:非法模式字符'T'.

我需要在java中解析一个字符串.我的字符串具有以下格式:

2014-09-17T12:00:44.0000000Z
Run Code Online (Sandbox Code Playgroud)

但Java试图解析这种格式时抛出以下异常... java.lang.IllegalArgumentException: Illegal pattern character 'T'.

关于如何解析的任何想法?

谢谢!

java string date simpledateformat

11
推荐指数
2
解决办法
2万
查看次数

将`Java.lang.String`转换为`oracle.sql.TIMESTAMPTZ`

我有以下Java.lang.String代表String值的值TIMESTAMPTZ.我需要转换这些Java.lang.String TO oracle.sql.TIMESTAMPTZ.

"2016-04-19 17:34:43.781 Asia/Calcutta",
"2016-04-30 20:05:02.002 8:00",
"2003-11-11 00:22:15.0 -7:00",
"2003-01-01 02:00:00.0 -7:00",
"2007-06-08 15:01:12.288 Asia/Bahrain",
"2016-03-08 17:17:35.301 Asia/Calcutta",
"1994-11-24 11:57:17.303"
Run Code Online (Sandbox Code Playgroud)

我试过很多方面.

样本1:

通过使用尝试 SimpleDateFormat

String[] timeZoneValues = new String[]{"2016-04-19 17:34:43.781 Asia/Calcutta", "2016-04-30 20:05:02.002 8:00", "2003-11-11 00:22:15.0 -7:00", "2003-01-01 02:00:00.0 -7:00", "2007-06-08 15:01:12.288 Asia/Bahrain", "2016-03-08 17:17:35.301 Asia/Calcutta", "1994-11-24 11:57:17.303"};
        for(String timeZoneValue: timeZoneValues){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS XXX");
            try {
                simpleDateFormat.parse(timeZoneValue);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } …
Run Code Online (Sandbox Code Playgroud)

java timezone oracle11g simpledateformat timestamp-with-timezone

11
推荐指数
1
解决办法
1027
查看次数