我目前正致力于将日期转换为字符串.之后我将该字符串转换为datetime.但它错了.有人可以帮帮我吗?
这是代码.
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
SimpleDateFormat outFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String dt1 = outFormat.format(date1);
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(dt1);
Run Code Online (Sandbox Code Playgroud) 我是Java新手.我在数据库中从数据库中检索我的第一列,表示数据为:
2014-09-01 10:00:00.000
Run Code Online (Sandbox Code Playgroud)
现在我想只显示时间:
10:00:00
Run Code Online (Sandbox Code Playgroud)
怎么做?我检索我的列的代码是:
public String[] getChartTime() throws SQLException {
List < String > timeStr = new ArrayList < String > ();
String atime[] = null;
getConnection();
try {
con = getConnection();
String sql = "exec vcs_gauge @gauge_name=?,@first_rec_time=?,@last_rec_time=?";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("date is " + df.format(currentDate));
clstmt = con.prepareCall(sql);
clstmt.setString(1, "vs3_bag");
clstmt.setString(2, "2014-09-01 10:00:00");
clstmt.setString(3, "2014-09-01 11:00:00");
clstmt.execute();
rs = clstmt.getResultSet();
while (rs.next()) {
// Just get the value of the column, and add it …Run Code Online (Sandbox Code Playgroud)我想以AM/PM格式显示时间.示例:上午9:00我也想执行加法减法运算.我的活动将从上午9:00开始.我想添加分钟来获得结果计划事件.除了制作自定义Time类之外,我该怎么办?
开始9:00 AM添加45分钟,添加开始时间9:45 AM
我以这种格式记录了一个日期
2014-12-09 02:18:38
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为
09-12-2014 02:18:38
Run Code Online (Sandbox Code Playgroud)
我尝试过这种方式转换
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TestDate {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-YYYY HH:mm:ss");
String input = "2014-12-09 02:18:38";
String strDate = sdf.format(input);
System.out.println(strDate);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我在运行时遇到了这个异常
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(DateFormat.java:301)
at java.text.Format.format(Format.java:157)
at TestDate.main(TestDate.java:15)
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题.
我从JSON API获得一个日期,看起来像这样的"2018-04-10T04:00:00.000Z".我想转换它以获得Date或String对象并获得类似"01-04-2018"的"dd-MM-YYYY".我该怎么做?
请告诉我如何解析这个日期:"2012年7月29日"
我尝试:
new SimpleDateFormat("dd-MMM-yyyy");
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我得到以下异常:
java.text.ParseException: Unparseable date: "29-July-2012"
Run Code Online (Sandbox Code Playgroud) 我将有一个字符串输入的样式:
yyyy-MM-dd'T'HH:mm:ssZ
是否可以将此字符串转换为日期,然后再将其解析为可读的dd-MM-yyyy日期对象?
例如:2013-08-11T17:22:04.51+01:00
在此stackoverflow 答案中,涵盖了不带.51部分的ISODateTime 。
请帮助纠正这个正则表达式
^(?:[1-9]\d{3}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1\d|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)-02-29)T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:Z|[+-][01]\d:[0-5]\d)$
Run Code Online (Sandbox Code Playgroud)
处理我的格式。
我正在尝试使用以下代码将字符串转换Wed July 2019 10:53 PM为LocalDateTime对象:
String dateAndTimeAsStr = "Wed July 2019 10:53 PM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMMM yyyy h:mm a");
LocalDateTime dateAndTimeAsLocalDateTime = LocalDateTime.parse(dateAndTimeAsStr, formatter);
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,出现以下错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Wed July 2019 10:53 PM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfWeek=3, MonthOfYear=7, Year=2019},ISO resolved to 22:53 of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)
更改yyyy到YYYY并h以hh没有取得任何不同的结果。
我究竟做错了什么?
谢谢
基于具有以下格式的输入字符串:"yyyyMMdd"例如"20170412",如何显示日期和日期?
例如:
input -> "20170412"
Run Code Online (Sandbox Code Playgroud)
output -> "Wednesday, April 12th 2017"或"Wed Apr 12 2017"类似的东西
无论我尝试使用哪种模式SimpleDateFormat(/sf/answers/295173721/),我总能得到我不需要的时间和时区.
那可能吗?
编辑:我的问题不重复.我在帖子中明确提到,所有模式SimpleDateFormat都列在可能的重复问题中,显示用户的时间和时区.我需要一个用户区域设置日期的模式,没有时间或时区.