我有问题SimpleDateFormat。
SimpleDateFormat dtfmt=new SimpleDateFormat("dd MMM yyyy hh:mm a", Locale.getDefault());
Date dt=dtfmt.parse(deptdt);
Run Code Online (Sandbox Code Playgroud)
在 Android 模拟器中工作正常,但在手机中出现此错误:
W/System.err: java.text.ParseException: Unparseable date: "24 Oct 2016 7:31 pm" (at offset 3) W/System.err: at java.text.DateFormat.parse(DateFormat.java:579)
有什么解决办法吗?
我正在尝试将数据从字符串转换为Data类,以便以后可以将其与其他数据进行比较。
我的数据格式:(dd-MM-yyyy例如31-07-2019)。
问题在于format.parse("string date")操作后它显示了错误的数据格式:
Wed Jul 31 00:00:00 UTC 2019
这是我的代码:
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.*;
public class Program {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
try {
String dateString = format.format(new Date());
String dateStr = "31-07-2019";
Date date = format.parse(dateStr);
System.out.println(dateString);
System.out.println(date);
} catch (ParseException e) {
System.out.println("ParseError " + e.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
dateString (当前日期)成功解析。
从https://api.spacexdata.com/v3/launches获取日期 该日期的格式为:2006-03-25T10:30:00+12:00。我想将其转换为“dd,mm,yyyy”,但总是收到错误:“java.time.format.DateTimeParseException:文本'2006-03-25T10:30:00 + 12:00'无法解析,未解析的文本发现于索引 10"
我的代码:
val formatter = DateTimeFormatter.ofPattern("dd, mm, yyyy", Locale.US)
val myDate = LocalDate.parse(launchDate, formatter)
var launchDateConverted: String= myDate.toString()
Run Code Online (Sandbox Code Playgroud)
我在字符串中获取数据,然后将其转换为日期进行格式化,然后将日期转换回字符串以在 UI 上显示。我使用了不同的方法,但找不到正确的方法。我当前的语言环境是“RU”。
我得到日期为 java.util.Date(not String) : (java.util.Date) Mon Jul 13 00:00:00 IST 2020
我想将其转换为:2020-07-13T00:00 格式==>("yyyy-MM-dd'T'HH:mm") 但作为日期而不是字符串。
我尝试了以下代码:
Date scheduleDate=details.getScheduledDate(); // This value is fetched from object passed-- [scheduleDate = (java.util.Date) Mon Jul 13 00:00:00 IST 2020]
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
sd.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
String dateFormat=sd.format(scheduleDate); //Here I get [dateFormat = (java.lang.String) "2020-07-13T00:00"]
Date date = sd.parse(dateFormat); //[date = (java.util.Date) Mon Jul 13 00:00:00 IST 2020]
Run Code Online (Sandbox Code Playgroud)
我观察到字符串格式具有正确的(如预期的)值,但是当我将其转换为 java.util.date 时,该值又变回来了。
java.util.Date 是否支持yyyy-MM-dd'T'HH:mm格式?
如果是的话,任何人都可以向我建议任何好的方法/方向/主题/图书馆来研究。
谢谢你..
我的要求是将字符串"2019-04-25 07:06:42.790"转换为与"2019-04-25 07:06:42.790"Date格式相同的对象。
我尝试这样做,但它总是以字符串格式给出。
String createDate = "2019-04-25 07:06:42.790";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
LocalDateTime localDateTime = LocalDateTime.parse(createDate, formatter);
System.out.println(formatter.format(localDateTime));
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
formatter1.setTimeZone(TimeZone.getTimeZone("IST"));
Date date = formatter1.parse(createDate);
System.out.println(date);
String formattedDateString = formatter1.format(date);
System.out.println(formattedDateString);
Run Code Online (Sandbox Code Playgroud)
上述代码的输出:
2019-04-25 07:06:42.790
Thu Apr 25 07:06:42 IST 2019
2019-04-25 07:06:42.790
Run Code Online (Sandbox Code Playgroud) 我需要一些支持。我希望将变量字符串转换为日期。变量日期的格式应为 dd-MM-yyyy。
import java.util.Date;
....
...
String a = "2022-05-12";
Date b; // should be dd-MM-yyyy
do some to format...
return b; // return b with format dd-MM-yyyy, remember this variable is type Date no String
Run Code Online (Sandbox Code Playgroud)
我试图做一些事情,但获得的格式不是所需的格式。
我的源日期类型是EEE MMM dd HH:mm:ss zzz yyyy. 例如,Sat Dec 12 00:00:00 KST 2020但我的目标日期类型是格式为 Java 日期类型yyyy-MM-dd。所以我制作了将Java String类型转换EEE MMM dd HH:mm:ss zzz yyyy为java util对象的方法。
private static Date getDate(String beforeDate) throws Exception{
SimpleDateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date rdate = readFormat.parse(beforeDate);
SimpleDateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
String format = writeFormat.format(rdate);
System.out.println(format);
return writeFormat.parse(format);
}
Run Code Online (Sandbox Code Playgroud)
System.out.println(format) line 打印出我期望的正确值。
2020-12-12
2020-12-12
2013-01-01
Run Code Online (Sandbox Code Playgroud)
但是返回值的类型是错误的。该方法的返回值似乎没有受到影响。
System.out.println(getDate("Sat Dec 12 00:00:00 KST 2020"));
Run Code Online (Sandbox Code Playgroud)
上一行Sat …