可能的解决方案:将Java Date转换为另一个Time as Date格式
我经历过但没有得到答案.
我有一个字符串" 2013-07-17T03:58:00.000Z ",我想将它转换为我们在制作新Date()时获得的相同形式的日期.日期d =新Date();
时间应该在IST.Zone-Asia/Kolkata
因此,上面字符串的日期应该是
7月17日星期三12:05:16 IST 2013 //无论什么时间按照印度标准GMT + 0530
String s="2013-07-17T03:58:00.000Z";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
TimeZone tx=TimeZone.getTimeZone("Asia/Kolkata");
formatter.setTimeZone(tx);
d= (Date)formatter.parse(s);
Run Code Online (Sandbox Code Playgroud) 我们运行Java 1.4.
我们有这个方法:
static SimpleDateFormat xmlFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
public static Date fromXml(String xmlDateTime) {
ParsePosition pp = new ParsePosition(0);
return xmlFormatter.parse(xmlDateTime, pp);
}
Run Code Online (Sandbox Code Playgroud)
其中xmlDateTime = 2013-08-22T16:03:00的例子.这一直有效,但突然停止了!
我们现在得到这个例外:
java.lang.ArrayIndexOutOfBoundsException: -1
at java.text.DigitList.fitsIntoLong(DigitList.java:170)
at java.text.DecimalFormat.parse(DecimalFormat.java:1064)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1381)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1159)
Run Code Online (Sandbox Code Playgroud)
我试图通过使用不同的日期格式在单元测试中重现这一点,即:
2013-08-22T16:03:00
2013-08-22 16:03:00
Run Code Online (Sandbox Code Playgroud)
但没有运气!有任何想法吗?
我想格式化的字符串如下所示:String datetime ="9/1/10 11:34:35 AM"
以下SimpleDateFormat模式的工作原理:
SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss");
Date d = sdf.parse(datetime);
System.out.println(d);
Output> [Wed Sep 01 11:34:35 CEST 2010]
Run Code Online (Sandbox Code Playgroud)
但是我也需要解析AM/PM标记,当我将其添加到模式时,我收到一个异常.
无效的模式:
SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss a");
Run Code Online (Sandbox Code Playgroud)
我也试过这个也有同样的例外:
SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss aa");
Run Code Online (Sandbox Code Playgroud)
例外:
java.text.ParseException: Unparseable date: "9/1/10 11:34:35 AM"
Run Code Online (Sandbox Code Playgroud)
我查看了http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#text上的API,但似乎无法找到我做错的地方.
有什么建议?
我试图解析一个约会,但我奇怪地得到一个例外.
这是代码:
import java.util.Date;
String strDate = "Wed, 09 Feb 2011 12:34:27";
Date date;
SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
try {
date = FORMATTER.parse(strDate.trim());
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
例外是:
java.text.ParseException:无法解析的日期:"Wed,09 Feb 2011 12:34:27",位于DateTest.main的java.text.DateFormat.parse(DateFormat.java:337)(DateTest.java:17)
我已阅读文档,我认为我的模式是正确的.所以我不明白......
任何的想法?
谢谢!
我正在尝试使用Java的SimpleDateFormat来解析使用以下代码的String.
public class DateTester {
public static void main(String[] args) throws ParseException {
String dateString = "2011-02-28";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
System.out.println(dateFormat.parse(dateString));
}
}
Run Code Online (Sandbox Code Playgroud)
我期待一些解析错误.但有趣的是,它会打印以下字符串.
Wed Jul 02 00:00:00 IST 195
Run Code Online (Sandbox Code Playgroud)
无法推理出来.有人可以帮忙吗?
谢谢
我正在使用代码将字符串格式化为日期
String start_dt = '2011-01-01';
DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
Date date = (Date)formatter.parse(start_dt);
Run Code Online (Sandbox Code Playgroud)
但是如何将日期从YYYY-MM-DD格式转换为MM-DD-YYYY格式?
可以在2012年1月4日删除"0"吗?
我目前正在使用以下Java来获取日期格式
Monday, January 04, 2012
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像
Monday, January 4, 2012
Date anotherCurDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EEEE', 'MMMM dd', ' yyyy");
final String formattedDateString = formatter.format(anotherCurDate);
final TextView currentRoomDate = (TextView) this.findViewById(R.id.CurrentDate);
Run Code Online (Sandbox Code Playgroud) 我有一个Json时间戳,我想使用javascript转换为简单的日期时间格式.
我需要以下格式的日期和时间:dd-mm-yyyy hr:mn
以下是我希望提取时间戳的json日期示例:"timestamp":1326439500
{
"count": 2,
"d": [
{
"title": "Apple iPhone 4S Sale Cancelled in Beijing Amid Chaos (Design You Trust)",
"description": "Advertise here with BSA Apple cancelled its scheduled sale of iPhone 4S in one of its stores in China’s capital Beijing on January 13. Crowds outside the store in the Sanlitun district were waiting on queues overnight. There were incidents of scuffle between shoppers and the store’s security staff when shoppers, hundreds of them, were told …Run Code Online (Sandbox Code Playgroud) 我已经用这段代码为Android应用程序挣扎了一段时间,我无法理解它.我已经阅读并尝试了我在stackoverflow和其他地方找到的每个解决方案,但仍然没有运气.
我想要做的是有一个函数将字符串转换为"17.08.2012 05:35:19:7600000"UTC日期和一个函数,该函数接受UTC date并将其转换为类似的字符串.
String value = "17.08.2012 05:35:19:7600000";
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSSSSSS");
try
{
Date today = df.parse(value);
System.out.println("Today = " + df.format(today) + " " + today.toGMTString());
}
catch (ParseException e)
{
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这导致:Today = 17.08.2012 07:41:59:0000000 17 Aug 2012 04:41:59 GMT哪都是错误的.
我试着SDF's timezone去UTC,没有运气.
我注意到的另一件事:如果我这样做,df.setLenient(false);
它会让我:java.text.ParseException: Unparseable date: "17.08.2012 05:35:19:7600000".
如果有人能提供一些解释/示例代码,我将非常感激.提前致谢
如果你能想象的话,这会在我的软件中引起Y2K风格的错误.奇怪的是,一年的计算只发生在一年中的两天,我不太确定如何排除故障.
输出:
03-Jan-2013
02-Jan-2013
01-Jan-2013
31-Dec-2013 ** strange
30-Dec-2013 ** strange
29-Dec-2012
28-Dec-2012
27-Dec-2012
26-Dec-2012
25-Dec-2012
Run Code Online (Sandbox Code Playgroud)
我不确定Java日期实用程序的哪一部分可能导致此类错误.
代码(因为测试很小,我包括一个完整的工作程序):
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateT {
private static String getFormattedBackscanStartTime(int days) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-YYYY");
Calendar workingDate = Calendar.getInstance();
workingDate.add(Calendar.DATE, -1 * days);
String formattedStartTime = dateFormat.format(workingDate.getTime());
return formattedStartTime;
}
public static void main(String args[]) {
for(int i = 35; i < 45; i++) {
System.out.println(getFormattedBackscanStartTime(i));
}
}
}
Run Code Online (Sandbox Code Playgroud) simpledateformat ×10
java ×9
date ×4
android ×2
datetime ×2
calendar ×1
date-parsing ×1
format ×1
javascript ×1
json ×1
timestamp ×1