以下测试失败:
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取代所有东西.某处有一堂课.
处理日期/月份可能有一位或两位数的日期的最佳方法是什么?
我有以下内容DateFormat:
yyyy-MM-dd HH:mm:ss.SSS
Run Code Online (Sandbox Code Playgroud)
我正在处理的应用程序运行良好,直到有一天它因为收到这样的日期而崩溃了:
2012-03-5 00:00:00.001 (Notice the single digit day).
Run Code Online (Sandbox Code Playgroud)
我偶然发现了这一点:使用java.text.SimpleDateFormat解析可能的单位数月/日/小时,但它没有回答我的问题.
我是否真的需要对日期字符串进行一些调整?或者还有其他更简洁的方式吗?
谢谢.
我正在尝试创建一个只接受24小时时间的JFormattedTextField.
我非常接近解决方案,但有一个案例,其中以下代码示例不起作用.
如果输入时间"222"并从字段中更改焦点,则时间将更正为"2202".我希望它只接受一个完整的4位数24小时时间.除了我刚刚提到的那个代码之外,这个代码几乎在所有情况下都能正常工作.有什么建议?
public static void main(String[] args) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("HHmm");
dateFormat.setLenient(false);
DateFormatter dateFormatter = new DateFormatter(dateFormat);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFormattedTextField textField = new JFormattedTextField(dateFormatter);
frame.add(textField, BorderLayout.NORTH);
frame.add(new JTextField("This is here so you can change focus."), BorderLayout.SOUTH);
frame.setSize(250, 100);
frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将字符串解析为日期,这就是我所拥有的:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zZ (zzzz)");
Date date = new Date();
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
要解析的字符串是这样的:
Sun Jul 15 2012 12:22:00 GMT+0300 (FLE Daylight Time)
Run Code Online (Sandbox Code Playgroud)
我关注了http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
很确定我已经完成了这本书的所有内容.但是它给了我ParseException.
java.text.ParseException: Unparseable date:
"Sun Jul 15 2012 12:22:00 GMT+0300 (FLE Daylight Time)"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我试过的模式:
EEE MMM dd yyyy HH:mm:ss zzz
EEE MMM dd yyyy HH:mm:ss zZ (zzzz)
Run Code Online (Sandbox Code Playgroud) 我按照以下代码在指定的dateTime上使用指定的Timezone创建Date对象.
注意:我没有为jvm设置任何时区; 但是使用不同的Linux服务器时区测试此代码.
String date = "20121225 10:00:00";
String timeZoneId = "Asia/Calcutta";
TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
DateFormat dateFormatLocal = new SimpleDateFormat("yyyyMMdd HH:mm:ss z");
//This date object is given time and given timezone
java.util.Date parsedDate = dateFormatLocal.parse(date + " "
+ timeZone.getDisplayName(false, TimeZone.SHORT));
if (timeZone.inDaylightTime(parsedDate)) {
// We need to re-parse because we don't know if the date
// is DST until it is parsed...
parsedDate = dateFormatLocal.parse(date + " "
+ timeZone.getDisplayName(true, TimeZone.SHORT));
}
Run Code Online (Sandbox Code Playgroud)
现在parsedDate对象的行为不同
当我的jvm服务器在IST中运行时
parsedDate.getTime() - 1356409800000
parsedDate.toString() …
我只想解析一个简单的时间!这是我的代码:
String s = "01:19 PM";
Date time = null;
DateFormat parseFormat = new SimpleDateFormat("hh:mm aa");
try {
time = parseFormat.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
java.text.ParseException: Unparseable date: "01:19 PM"
at java.text.DateFormat.parse(Unknown Source)
Run Code Online (Sandbox Code Playgroud) 我一直在寻找这个,到目前为止没有成功.你知道是否有一个"DateFormat"ish类,这将允许我使用"00:00:00"和"24:00:00"作为输入参数(它们都是午夜)但是当被称为"getHour()"时"结果我会得到0或24?
使用"kk"只允许我有<1:24>范围,同时我正在寻找<0:24>范围格式
我试图2014-12-03T10:05:59.5646+08:00使用这两种格式解析日期:
yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd'T'HH:mm:ssXXX当我解析使用yyyy-MM-dd'T'HH:mm:ss它工作正常,但当我解析yyyy-MM-dd'T'HH:mm:ssXXX一个ParseException被抛出.
哪种解析日期的格式正确,以及这两种格式之间究竟有什么区别?
注意:我不能用Joda :(
如果输入01-01-2015它应该改为2015-01-01.
如果输入2015-01-01它应该改为01-01-2015.
我使用SimpleDateFormat但没有得到正确的输出:
//Class to change date dd-MM-yyyy to yyyy-MM-dd and vice versa
public class ChangeDate {
static SimpleDateFormat formatY = new SimpleDateFormat("yyyy-MM-dd");
static SimpleDateFormat formatD = new SimpleDateFormat("dd-MM-yyyy");
//This function change dd-MM-yyyy to yyyy-MM-dd
public static String changeDtoY(String date) {
try {
return formatY.format(formatD.parse(date));
}
catch(Exception e) {
return null;
}
}
//This function change yyyy-MM-dd to dd-MM-yyyy
public static String changeYtoD(String date) {
try {
return formatD.format(formatY.parse(date));
} …Run Code Online (Sandbox Code Playgroud) 已解决我发现在午夜和凌晨1点之间,我的设备返回1小时后的时间(其他23个小时,它正确返回).更奇怪的是,如果我使用kk而不是HH(因为结果字符串对我没用),它会正确返回
我正在运行的代码:(在这个实例中strFormat匹配硬编码的字符串df3)
SimpleDateFormat df = new SimpleDateFormat(strFormat, Locale.US);
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd kk:mm", Locale.US);
SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
Date d = c.getTime();
String s = d.toString();
String ret = df.format(c.getTime());
String ret2 = df.format(new Date(System.currentTimeMillis()));
String ret3 = df2.format(c.getTime());
String ret4 = df3.format(c.getTime());
String r1 = ""+c.get(Calendar.HOUR);
Run Code Online (Sandbox Code Playgroud)
这些回报:
s = "Thu Jan 07 00:39:32 GMT-11:00 2016"
ret = "2016-01-07 01:39:32"
ret2 = "2016-01-07 01:39:32"
ret3 = "2016-01-07 24:39" …Run Code Online (Sandbox Code Playgroud) java ×10
simpledateformat ×10
date ×4
date-format ×2
android ×1
java-time ×1
jodatime ×1
string ×1
swing ×1
timezone ×1