135*_*355 200 java calendar date converter
如何将日历日期转换为yyyy-MM-dd格式.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
Date date = cal.getTime();
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String date1 = format1.format(date);
Date inActiveDate = null;
try {
inActiveDate = format1.parse(date1);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这将产生inActiveDate = Wed Sep 26 00:00:00 IST 2012.但我需要的是2012-09-26.我的目的是使用Hibernate标准将此日期与我的数据库中的另一个日期进行比较.所以我需要yyyy-MM-dd格式化的日期对象.
Mad*_*mer 332
Java Date是自1970年1月1日00:00:00 GMT以来的毫秒数的容器.
当你使用类似的东西时System.out.println(date),Java Date.toString()用来打印内容.
改变它的唯一方法是覆盖Date并提供自己的实现Date.toString().在你启动你的IDE之前尝试这个,我不会; 这只会使问题复杂化.最好将日期格式化为要使用(或显示)的格式.
LocalDateTime ldt = LocalDateTime.now().plusDays(1);
DateTimeFormatter formmat1 = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
System.out.println(ldt);
// Output "2018-05-12T17:21:53.658"
String formatter = formmat1.format(ldt);
System.out.println(formatter);
// 2018-05-12
Run Code Online (Sandbox Code Playgroud)
你应该使用ThreeTen Backport
以下是为历史目的而维护(作为原始答案)
您可以做的是格式化日期.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(cal.getTime());
// Output "Wed Sep 26 14:23:28 EST 2012"
String formatted = format1.format(cal.getTime());
System.out.println(formatted);
// Output "2012-09-26"
System.out.println(format1.parse(formatted));
// Output "Wed Sep 26 00:00:00 EST 2012"
Run Code Online (Sandbox Code Playgroud)
这些实际上是相同的日期,表示不同.
Nir*_*oon 19
你的代码错了.无需解析日期并将其保留为Date对象.
您可以在要显示时格式化日历日期对象,并将其保留为字符串.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
Date date = cal.getTime();
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String inActiveDate = null;
try {
inActiveDate = format1.format(date);
System.out.println(inActiveDate );
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
Bas*_*que 13
MadProgrammer的答案是正确的,尤其是关于Joda-Time的提示.Joda-Time的后继者现在作为新的java.time包构建到Java 8中.这是Java 8中的示例代码.
使用日期时间(而不是本地日期)时,时区是关键的.日期取决于时区.例如,印度标准时间是+05:30(UTC提前五个半小时),而法国是只领先一小时.所以在印度新的一天有一个约会,而在法国的同一时刻有"昨天"约会.创建缺少任何时区或偏移信息的字符串输出会产生歧义.您要求YYYY-MM-DD输出,所以我提供了,但我不推荐它.而不是ISO_LOCAL_DATE我会习惯ISO_DATE得到这个输出:2014-02-25+05:30
ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zonedDateTime = ZonedDateTime.now( zoneId );
DateTimeFormatter formatterOutput = DateTimeFormatter.ISO_LOCAL_DATE; // Caution: The "LOCAL" part means we are losing time zone information, creating ambiguity.
String output = formatterOutput.format( zonedDateTime );
Run Code Online (Sandbox Code Playgroud)
转储到控制台......
System.out.println( "zonedDateTime: " + zonedDateTime );
System.out.println( "output: " + output );
Run Code Online (Sandbox Code Playgroud)
跑的时候......
zonedDateTime: 2014-02-25T14:22:20.919+05:30[Asia/Kolkata]
output: 2014-02-25
Run Code Online (Sandbox Code Playgroud)
使用Joda-Time库的类似代码,java.time的前身.
DateTimeZone zone = new DateTimeZone( "Asia/Kolkata" );
DateTime dateTime = DateTime.now( zone );
DateTimeFormatter formatter = ISODateTimeFormat.date();
String output = formatter.print( dateTime );
Run Code Online (Sandbox Code Playgroud)
顺便说一句,输入字符串的格式是标准格式,是ISO 8601定义的几种方便的日期时间字符串格式之一.
在解析和生成各种日期时间值的字符串表示时,Joda-Time和java.time默认使用ISO 8601格式.
java.util.Date对象不能以自定义格式表示日期,而是使用返回的方法.SimpleDateFormat.formatstring
String myString=format1.format(date);
Run Code Online (Sandbox Code Playgroud)
小智 6
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(year, month, date);
SimpleDateFormat format1 = new SimpleDateFormat("yyyy MM dd");
String formatted = format1.format(cal.getTime());
System.out.println(formatted);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
607319 次 |
| 最近记录: |