标签: simpledateformat

Java String to Date,ParseException

我有一个名为的字符串DateCompareOld, it has the value "Fri Aug 12 16:08:41 EDT 2011".我想将其转换为日期对象.

 SimpleDateFormat dateType =  new SimpleDateFormat("E M dd H:m:s z yyyy");
 Date convertDate = dateType.parse(DateCompareOld);
Run Code Online (Sandbox Code Playgroud)

但每次我尝试这个,我得到一个解析异常.我尝试过其他SimpleDateFormat格式标准,但总是失败.

建议?

java date object simpledateformat

0
推荐指数
1
解决办法
2249
查看次数

解析后SimpleDateFormat错误的unix时间戳

我正在尝试使用SimpleDateFormat在unix时间戳和自定义格式之间进行转换,反之亦然.为了测试这个反之亦然转换我编写了以下测试用例:

public class Testception extends TestCase {
    public void testTheTestOrTestception() throws ParseException {
        Date datum = new Date(649555200000L);
        SimpleDateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
        TimeZone tZ = TimeZone.getTimeZone("Europe/Berlin");
        dfm.setTimeZone(tZ);
        String aDifferentFormat = dfm.format(datum);
        assertEquals("1990-08-02", aDifferentFormat);
        Date datum2 = dfm.parse(aDifferentFormat);
        assertEquals(649555200000L, datum2.getTime());
    }
}
Run Code Online (Sandbox Code Playgroud)

我开始使用unixtimestamp(649555200000),以我的自定义格式("1990-08-02")转换它,这很好用.但不幸的是,第二个断言失败,而不是预期的649555200000L,结果是649548000000L

提前致谢.

干杯L0rdAli3n

java timezone date simpledateformat

0
推荐指数
1
解决办法
1728
查看次数

解析带有空格的String到Date,导致ParseException

我有这种形式的日期字符串 Thu Aug 02 00:00:00 GMT+00:00 2012

我试图使用此方法来解析Date对象中的这些String

public Date fromStringToDate(String data) {
        Date result;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
        try {
            result = sdf.parse(data);
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是没有用,我得到了这个错误

java.text.ParseException: Unparseable date: "Thu Aug 02 00:00:00 GMT+00:00 2012"
Run Code Online (Sandbox Code Playgroud)

我认为问题是由错误引起的SimpleDateFormat,但我不知道正确的语法来修复它.

java android date simpledateformat

0
推荐指数
1
解决办法
586
查看次数

如何使用类型为2015-01-13T10:24:55Z的java中的SimpleDateFormat来解析日期

我有以下日期字符串来自Android应用程序中的grails服务.

2015-01-13T10:24:55Z
Run Code Online (Sandbox Code Playgroud)

现在我想解析这个字符串使用SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"); 但我在logcat中得到错误跟随错误.

01-13 10:53:50.674: W/System.err(1794): java.text.ParseException: Unparseable date: "2015-01-13T10:24:55Z" (at offset 10)
Run Code Online (Sandbox Code Playgroud)

请让我知道如何解析这种类型的字符串.

java simpledateformat

0
推荐指数
1
解决办法
484
查看次数

java中的日期转换?

我有这个日期:2016年2月23日下午4:28:46

String d="2/23/2016 4:28:46 PM";
Format formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
try {
    Date date = ((DateFormat) formatter).parse(d);
    formatter = new SimpleDateFormat("dd MMM HH:mm a");
    String notDate = formatter.format(date);
    holder.tvNotificationTime.setText(notDate);
    System.out.println(notDate);
} catch (ParseException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

我希望输出为

23 Feb 4:28 PM
Run Code Online (Sandbox Code Playgroud)

但我输出为

23 Feb 04:28 am
Run Code Online (Sandbox Code Playgroud)

这有什么不对?

java date simpledateformat

0
推荐指数
1
解决办法
51
查看次数

无法在java中将字符串dd/MM/yyyy转换为Date dd/MM/yyyy

我有一个格式的输入字符串dd/MM/yyyy,我需要将其转换为日期dd/MM/yyyy.

我的方法是:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String date = formatter.format(formatter.parse("22/09/2016"));
Date convertedDate = formatter.parse(date);
Run Code Online (Sandbox Code Playgroud)

我期待22/09/2016作为日期对象,但返回的格式不是预期的.O/P=>Mon Sep 12 00:00:00 IST 2016

知道我哪里错了吗?提前致谢!

java date-format simpledateformat

0
推荐指数
1
解决办法
825
查看次数

如何从Java中的String格式获取年,月,日

我正在从事一项工作,我需要从Java中的String中获取年,月,日.字符串是这样的:20170119T163048

2017年为年,01为月,19为日,16:30:48为时间.

我实现了如下代码:

public void convertStringToDate (String string) {
    String dateInString = "";   
    SimpleDateFormat formatter = new SimpleDateFormat();
    Date date = formatter.parse("20170119T163048");
    System.out.println(date);
    }
Run Code Online (Sandbox Code Playgroud)

但似乎它不起作用.环顾四周,我发现了很多相似的答案,但没有一个真正适合我,所以......

java datetime date simpledateformat

0
推荐指数
1
解决办法
972
查看次数

Java simpledateformat月返回零

这可能是重复的,但我无法弄清为什么当将MMM指定为MMM并与mm(数字)一起使用时,该月返回零。这里的任何帮助将不胜感激?

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Locale;

 public class time1 {

 public static void main(String[] args) {

    DateFormat originalFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
    DateFormat targetFormat = new SimpleDateFormat("yyyy-mm-dd");
    Date date = null;
    try {
            date = originalFormat.parse("26-Aug-2011");
    } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
    String formattedDate = targetFormat.format(date);
    System.out.println("old date: " + date);
    System.out.println("new date: " + formattedDate);
 }
}
Run Code Online (Sandbox Code Playgroud)

输出为:

旧日期:IST 2011年8月26日星期五00:00:00
新日期:2011 -00- 26年

当格式更改为dd-mm-yyyy且日期为26-08-2011时,输出为

旧日期:IST 2011年1月26日星期三00:07:00 …

java simpledateformat java-date

0
推荐指数
1
解决办法
1125
查看次数

解析日期显示错误的日期

我正在尝试将字符串解析为日期,但它显示错误的日期

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

public class Dateformat {
    public static void main(String[] args) throws Exception {
        String sDate1 = "2018-10-05T00:00:00-05:00";
        Date date1 = new SimpleDateFormat("yyyy-mm-dd").parse(sDate1);
        System.out.println(sDate1 + "\t" + date1);
    }
}
Run Code Online (Sandbox Code Playgroud)

怀疑:为什么即使它的模式与输入字符串不匹配呢?

其次,为什么它显示错误的日期?

Output : 2018-10-05T00:00:00-05:00  Fri Jan 05 00:10:00 IST 2018
Run Code Online (Sandbox Code Playgroud)

建议请

java date simpledateformat

0
推荐指数
1
解决办法
188
查看次数

Java SimpleDateformatter在秒后有10位小数,无法转换为Date

我试图将日期字符串转换为10毫秒(2018-11-02 6:05:59.1541162159 PM)到目前为止,但无法获得确切的日期.

要转换的代码:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateFormatCheck {
    private static TimeZone tz = TimeZone.getTimeZone("Asia/Colombo");
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS aa");

    public static void main(String[] a){
        try {
            Calendar cal = Calendar.getInstance(tz);
            sdf.setCalendar(cal);
            cal.setTime(sdf.parse("2018-11-02 6:05:59.1541162159 PM"));
            Date date = cal.getTime();
            System.out.println(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Tue Nov 20 02:12:01 IST 2018
Run Code Online (Sandbox Code Playgroud)

java milliseconds simpledateformat datetime-parsing

0
推荐指数
1
解决办法
198
查看次数