以下测试失败:
DateFormat df = new SimpleDateFormat("HH:mm:ss z");
assertEquals("00:00:00 GMT", df.format(new Date(0)));
Run Code Online (Sandbox Code Playgroud)
预计"00:00:00 GMT"但是"01:00:00 GMT"
有人能指出我傻到哪儿吗?
我花了更长的时间来看这个,而不是用Joda-Time取代所有东西.某处有一堂课.
我在整个网站上搜索过,但我认为我有一个稍微不同的问题,在我出现心力衰竭或烧坏计算机之前可以真正做些帮助.
我动态生成月份名称列表(以2011年6月,2011年7月的形式),显然我希望这对区域设置敏感:因此我使用简单的日期格式对象,如下所示:
//the actual locale name is dependent on UI selection
Locale localeObject=new Locale("pl");
// intended to return full month name - in local language.
DateFormat dtFormat = new SimpleDateFormat("MMMM yyyy",localeObject);
//this bit just sets up a calendar (used for other bits but here to illustrate the issue
String systemTimeZoneName = "GMT";
TimeZone systemTimeZone=TimeZone.getTimeZone(systemTimeZoneName);
Calendar mCal = new GregorianCalendar(systemTimeZone); //"gmt" time
mCal.getTime(); //current date and time
Run Code Online (Sandbox Code Playgroud)
但如果我这样做:
String value=dtFormat.format(mCal.getTime());
Run Code Online (Sandbox Code Playgroud)
这个"应该"返回月份名称的本地化版本.在波兰语中,"九月"这个词是"Wrzesień" - 请注意n上的重音.然而我回来的只是"Wrzesie?"
我究竟做错了什么?
感谢所有 - 我现在接受这是一个演示问题 - 但我怎样才能安全地"读取"dtFormat的结果 - 我使用getBytes等在ref下面添加了一些注释 …
我的报告页面使用 java date java.util.Calendar 和 java.text.SimpleDateFormat 。
我想始终将开始日期设置为上一个星期六,并将结束日期设置为该星期六之后的星期五。
我写了一个 Java 代码来检查这个并按如下方式执行,但我确信它的逻辑是错误的
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -7);
setToDate(sdf.format(cal.getTime()));
cal.add(Calendar.DATE, -6);
setFromDate(sdf.format(cal.getTime()));
Run Code Online (Sandbox Code Playgroud)
如何获取上一个 FromDate(yyyy-mm-dd) 和 ToDate(yyyy-mm-dd) 其中 FromDate 应该是上周六,ToDate 应该是上周五。


我是一名法国 Android 开发人员,因此使用Locale.getDefault()会导致我DateFormat使用 24 小时模式。但是,当我通过设置菜单DateFormat将设备手动设置为 12 小时模式时,会继续以 24 小时格式运行。
相反,TimePickers 是根据我自己的 12/24 小时设置来设置的。
有没有办法让DateFormats 的行为与 s 相同TimePicker?
编辑:
这是我的DateFormat声明:
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
Run Code Online (Sandbox Code Playgroud)
这是我将我设置TimePicker为 12 或 24 小时模式的地方。
tp.setIs24HourView(android.text.format.DateFormat.is24HourFormat((Context) this));
Run Code Online (Sandbox Code Playgroud)
我的解决方案:
根据下面@Meno Hochschild 的回答,这是我解决这个棘手问题的方法:
boolean is24hour = android.text.format.DateFormat.is24HourFormat((Context) this);
tp.setIs24HourView(is24hour); // tp is the TimePicker
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
if (timeFormat instanceof SimpleDateFormat) {
String pattern = ((SimpleDateFormat) timeFormat).toPattern();
if (is24hour) {
timeFormat = …Run Code Online (Sandbox Code Playgroud) 在标记重复之前,请仔细阅读问题.
我想要相应日期的前一个日期.(不是昨天的日期)
例如,如果用户点击按钮,他将被导航到另一个屏幕,并显示有关昨天的数据.
如果他再次点击该屏幕上的相同按钮,那么数据应该在前天显示....依此类推......直到我的数据库中存在数据.
所以我想得到相应日期的前一个日期.即如果我有2014年1月31日的日期(我使用格式31012014存储在数据库中),那么我应该得到日期30012014.
我知道如何获得昨天的日期,例如下面的代码
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy", Locale.getDefault());
String yesterdayAsString = dateFormat.format(calendar.getTime());
Run Code Online (Sandbox Code Playgroud)
给出了与今天相比的日期,但我希望以前的日期与其他有效日期相比较.
那么如何做到这一点.
我目前的日期格式是:08/11/2008 00:00
我需要将此输出转换为2008/11/08 00:00然而,使用SimpleDateFormat进行研究它无法这样做并给我一个完全不同的输出,这里是我的代码如下:
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm")
Date starting= simpleDateFormat2.parse(startTime);
System.out.println("" + simpleDateFormat2.format(starting) + " real date " + startTime);
Run Code Online (Sandbox Code Playgroud)
我知道我正在解析正确的字符串,因为发生以下输出:
0014/05/01 00:00 real date 08/11/2008 00:00
Run Code Online (Sandbox Code Playgroud)
我不太确定机械师如何检测到0014/05/01 00:00而不是
2008/11/08 00:00
我期待所有的建议提前谢谢
这段代码在Windows中正常工作,但在Linux中抛出java.text.ParseException:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("es", "ES"));
df.setLenient(false);
Date date = df.parse("1901-01-01 00:00:00");
System.out.println(date);
Run Code Online (Sandbox Code Playgroud)
Windows输出:
Tue Jan 01 00:00:00 CET 1901
Run Code Online (Sandbox Code Playgroud)
Linux输出:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.simontuffs.onejar.Boot.run(Boot.java:340)
at com.simontuffs.onejar.Boot.main(Boot.java:166)
Caused by: java.text.ParseException: Unparseable date: "1901-01-01 00:00:00"
at java.text.DateFormat.parse(DateFormat.java:357)
...
Run Code Online (Sandbox Code Playgroud)
如果删除该df.setLenient(false)行,则Windows输出相同,并且Linux异常消失,但Linux输出似乎不正确:
Tue Jan 01 00:14:44 CET 1901
Run Code Online (Sandbox Code Playgroud)
有人知道发生了什么吗?
谢谢
配置:
Windows:Win7 + jdk1.7.0_71
Linux:Ubuntu + jdk1.7.0_60
编辑:正如anolsi所说,这是一个夏令时问题.使用日期"2015-03-29 02:00:01",在Windows和Linux中抛出了解析异常,因为马德里不存在此日期(时间从马德里凌晨2:00更改为凌晨3:00那天).所以正确的行为是Linux.Windows JDK应抛出异常.
在我的 SQLite 数据库中,没有Date数据类型,所以我必须以文本格式存储时间戳。
格式是否会yyyy-MM-dd HH:mm导致正确的排序,以便当您按字典顺序对其进行排序(通过执行正常排序 ASC 或 DESC)时,它也固有地按时间值排序?
我正在尝试理解一些SimpleDateFormat代码.特别是我试图在SimpleDateFormat中使用本地化的模式字符串.来自javadoc:
SimpleDateFormat还支持本地化的日期和时间模式字符串.在这些字符串中,上述模式字母可以用其他依赖于语言环境的模式字母替换.
它还指定了一个SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)构造函数:
使用给定的模式和日期格式符号构造SimpleDateFormat.
但是,尽管getLocalPatternChars()实例呈现了预期的模式字符,但SimpleDateFormat的构造函数拒绝包含这些字符的模式:
public void run() {
Locale loc = new Locale("de", "de");
DateFormatSymbols dfs = new DateFormatSymbols(loc);
String sym = dfs.getLocalPatternChars();
System.out.println(sym);
SimpleDateFormat datefmt = new SimpleDateFormat("tt.MM.uuuu", dfs);
}
Run Code Online (Sandbox Code Playgroud)
产生输出:
GuMtkHmsSEDFwWahKzZ
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 't'
at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:845)
...
Run Code Online (Sandbox Code Playgroud)
如果我用" ... new SimpleDateFormat("tt.MM.uuuu", loc);" 替换最后一行,我得到相同的输出.
另一方面,如果我使用任何Anglicized模式字符串创建SimpleDateFormat实例,然后调用" applyLocalizedPattern("tt.MM.uuuu")",则接受本地化模式.
所以似乎不能在SimpleDateFormat的构造函数中使用本地化的模式字符串,并且需要这两步初始化.这是故意的行为吗?
我使用下面的代码来施法时间为UTC这是工作
import java.text.SimpleDateFormat;
import static java.util.Calendar.*
def dt = "2018-03-19T06:00:00+01:00"
def format = "yyyy-MM-dd'T'HH:mm:ssX"
TimeZone tz = TimeZone.getDefault(); //getting up local time zone
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = sdf.parse(dt);
TimeZone.setDefault(tz);
println d //output: 2018-03-19T05:00:00Z
println d.toTimestamp(); //Output: 2018-03-19 06:00:00.0
Run Code Online (Sandbox Code Playgroud)
但是当我使用TimeZone.setTimeZone(TimeZone.getTimeZone("UTC"));它时它不起作用。
它只与 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
为什么这样?
评论后更新:输出需要在 CET 但在 UTC
def dt = "2018-03-19T06:00:00+01:00"
def format = "yyyy-MM-dd'T'HH:mm:ssX"
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(TimeZone.getTimeZone("CET"))
Date d = sdf.parse(dt);
println d
println d.toTimestamp();
Run Code Online (Sandbox Code Playgroud)
? 输出:
Mon …Run Code Online (Sandbox Code Playgroud)