我想创建一个没有TimeZone的Date对象(例如:) 2007-06-21.这可能吗?
当我使用以下方法打印时Thu Jun 21 00:00:00 GMT 2007
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
TimeZone timeZone = TimeZone.getTimeZone("GMT");
timeZone.setDefault(timeZone);
sdf.setTimeZone(timeZone);
Date pickUpDate = sdf.parse("2007-06-21");
System.out.println(pickUpDate);
Run Code Online (Sandbox Code Playgroud) 我需要不同格式的字符串来转换为“DD.MM.YYYY”。
"Thu, 3 Nov 2022 06:00:00 +0100"必须改为"03.11.2022"
和
"01.11.2022 20:00:00"到"01.11.2022"。
所有格式均为String.
我尝试做
String pattern="DD.MM.YYYY";
DateTimeFormatter formatter=DateTimeFormatter.ofPattern(pattern);
new SimpleDateFormat(pattern).parse("01.11.2022 20:00:00")
Run Code Online (Sandbox Code Playgroud)
我也尝试过执行以下操作
java.time.LocalDateTime.parse(
item.getStartdatum(),
DateTimeFormatter.ofPattern( "DDMMYYYY" )
).format(
DateTimeFormatter.ofPattern("DD.MM.YYYY")
)
Run Code Online (Sandbox Code Playgroud)
但出现错误:
Exception in thread "main" java.time.format.DateTimeParseException:
Text 'Sun, 30 Oct 2022 00:30:00 +0200' could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)
我也尝试执行以下操作
Exception in thread "main" java.time.format.DateTimeParseException:
Text 'Sun, 30 Oct 2022 00:30:00 +0200' could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)
但是,我没有得到正确的输出。我怎样才能得到我想要的结果?
我有一个字符串,其中包含日期,我需要hh:mm:ss要添加到日期,但是当我使用dateFormat时它会给我ParseException.这是代码:
DateFormat sdff = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String startDate = "2013-09-25";
Date frmDate;
frmDate = sdff.parse(startDate);
System.out.println("from date = " + frmDate);
Run Code Online (Sandbox Code Playgroud)
我得到了abv代码的解析异常.但是,如果我从日期格式中删除hh:mm:ss它工作正常,输出将来自日期=星期三9月25日00:00:00 IST 2013.但我需要从date = 2013-09-25 00:00:00输出
请帮我.提前致谢.
有人可以帮助我进行这种转换吗?
String dateTime="20140505 03:23:50"
DateFormat formatter=new SimpleDateFormat("DD/MM/yyyy");
String date=formatter.format(dateTime);
Run Code Online (Sandbox Code Playgroud)
我希望输出采用这种格式 - "DD/MM/YYYY",它应该是一个字符串.
我有两个日期,需要格式化他们,但我收到以下异常.我的主要问题是th rd etc当天的部分.我找不到这个问题的答案.我检查了所有这些链接1,2,3,4,5,我想我应该用正则表达式,但不知道如何.
10th Dec 2019 -> 2019-12-10
10th December 2019 -> 2019-12-10
Run Code Online (Sandbox Code Playgroud)
码
String date1 = "10th Dec 2019";
Date date = new SimpleDateFormat("dd MMMM YYYY").parse(date1);
System.err.println(date);
String date2 = new SimpleDateFormat("yyyy-mm-dd").format(date);
System.err.println(date2);
Run Code Online (Sandbox Code Playgroud)
例外
Exception in thread "main" java.text.ParseException: Unparseable date: "10th Dec 2019"
Run Code Online (Sandbox Code Playgroud) 我试过这段代码,但它给了我错误格式的日期:
function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
randomDate(new Date(2012, 0, 1), new Date())
Run Code Online (Sandbox Code Playgroud)
返回这样的日期
Fri Dec 16 1988 06:20:22 GMT+0000 (GMT Standard Time)
Run Code Online (Sandbox Code Playgroud)
我如何将其更改为“yyyy-mm-dd”格式?所以例如
1985-09-22
Run Code Online (Sandbox Code Playgroud) 我试图将日期字符串08-12-2017转换为2017-12-08(LocalDate).这是我试过的 -
String startDateString = "08-12-2017";
LocalDate date = LocalDate.parse(startDateString);
System.out.println(date);
Run Code Online (Sandbox Code Playgroud)
还尝试使用formatter,但得到相同的结果,一个DateTimeParseException.如何在没有例外的情况下获得像2017-12-08这样的输出?
在Java 8项目中,我可以这样解析日期:
LocalDate.parse(
newDateString,
DateTimeFormatter.ofPattern(expectedDateFormat)
)
.format(
DateTimeFormatter.ofPattern(expectedDBDateFormat)
);
Run Code Online (Sandbox Code Playgroud)
我的问题是,我有一个Java 6的项目,LocalDate并DateTimeFormatter没有为Java 6的定义.
如何在Java 6项目中解析日期?
我有Date格式的Date对象
/107/2013 12:00:00 AM
Run Code Online (Sandbox Code Playgroud)
我的预期价值:
2013-07-01
Run Code Online (Sandbox Code Playgroud)
我怎么做的?
我正在尝试使用此代码
public static Date formatearFecha( Date fecha ){
String fechaString = new SimpleDateFormat("yyyy-MM-dd").format(fecha) ;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date fechaFormateada = null;
try {
fechaFormateada = df.parse(fechaString);
} catch (ParseException ex) {
Logger.getLogger(TestThings.class.getName()).log(Level.SEVERE, null, ex);
}
return fechaFormateada;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试进行测试时,我得到了这个:
System.out.println( formatearFecha(myDate) );
Mon Sep 30 00:00:00 COT 2013
Run Code Online (Sandbox Code Playgroud)
更新:
我的问题是在sql中将java.util.Date更改为java.sql.Date
我解决了这个问题:
private String formatearFecha( Date fecha ){
return new SimpleDateFormat("yyyy-MM-dd").format(fecha);
}
private java.sql.Date stringToSQLDate( String fecString ){
java.sql.Date fecFormatoDate = null; …Run Code Online (Sandbox Code Playgroud) 我正在尝试格式化具有date以下格式的键的JSON :
日期:“ 2017-05-23T06:25:50”
我当前的尝试如下:
private String formatDate(String date) {
SimpleDateFormat format = new SimpleDateFormat("MM/DD/yyyy");
String formattedDate = "";
try {
formattedDate = format.format(format.parse(String.valueOf(date)));
} catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,结果不会显示在我的应用中,它TextView是不可见的,并且我无法记录任何内容。
我确实尝试过其他解决方案,但对我而言没有成功。我不知道是否是因为传入的值是一个字符串。
java ×9
date ×8
datetime ×3
date-format ×2
string ×2
android ×1
date-parsing ×1
java-6 ×1
java-7 ×1
java-8 ×1
java-time ×1
javascript ×1
regex ×1
timezone ×1