我使用以下代码以“dd / MM / yyyy HH:mm:ss.SS”格式获取日期。
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateAndTime{
public static void main(String[] args)throws Exception{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
String strDate = sdf.format(cal.getTime());
System.out.println("Current date in String Format: "+strDate);
SimpleDateFormat sdf1 = new SimpleDateFormat();
sdf1.applyPattern("dd/MM/yyyy HH:mm:ss.SS");
Date date = sdf1.parse(strDate);
System.out.println("Current date in Date Format: "+date);
}
}
我正在获得以下输出
Current date in String Format: 05/01/2012 21:10:17.287
Current date in Date Format: Thu Jan 05 21:10:17 IST 2012
请建议我应该怎样做以相同的字符串格式显示日期(dd / MM / yyyy HH:mm:ss.SS),即我想要以下输出:
Current date in String Format: 05/01/2012 21:10:17.287
Current date in Date Format: 05/01/2012 21:10:17.287
请建议
答案 0 :(得分:16)
的SimpleDateFormat
sdf=new SimpleDateFormat("dd/MM/YYYY hh:mm:ss");
String dateString=sdf.format(date);
它会按预期提供输出28/09/2013 09:57:19
。
答案 1 :(得分:15)
你不能 - 因为你正在调用Date.toString()
总是包含系统时区,如果它是默认日期格式的默认语言环境。 Date
值本身没有格式的概念。如果您想以特定方式对其进行格式化,请使用SimpleDateFormat.format()
...使用Date.toString()
几乎总是一个坏主意。
答案 2 :(得分:12)
以下代码给出了预期的输出。你想要的吗?
import java.util.Calendar;
import java.util.Date;
public class DateAndTime {
public static void main(String[] args) throws Exception {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
String strDate = sdf.format(cal.getTime());
System.out.println("Current date in String Format: " + strDate);
SimpleDateFormat sdf1 = new SimpleDateFormat();
sdf1.applyPattern("dd/MM/yyyy HH:mm:ss.SS");
Date date = sdf1.parse(strDate);
String string=sdf1.format(date);
System.out.println("Current date in Date Format: " + string);
}
}
答案 3 :(得分:3)
使用:
System.out.println("Current date in Date Format: " + sdf.format(date));
答案 4 :(得分:1)
您的第一个打印行中的输出正在使用您的格式化程序。第二个输出(从解析后的字符串创建的日期)使用Date#toString输出,它根据自己的规则进行格式化。也就是说,你没有使用格式化程序。
规则是根据您在此处看到和描述的内容: http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#toString()
答案 5 :(得分:1)
Date
/ Calendar
/ SimpleDateFormat
类。示例:
ZonedDateTime // Represent a moment as seen in the wall-clock time used by the people of a particular region (a time zone).
.now( // Capture the current moment.
ZoneId.of( "Africa/Tunis" ) // Always specify time zone using proper `Continent/Region` format. Never use 3-4 letter pseudo-zones such as EST, PDT, IST, etc.
)
.truncatedTo( // Lop off finer part of this value.
ChronoUnit.MILLIS // Specify level of truncation via `ChronoUnit` enum object.
) // Returns another separate `ZonedDateTime` object, per immutable objects pattern, rather than alter (“mutate”) the original.
.format( // Generate a `String` object with text representing the value of our `ZonedDateTime` object.
DateTimeFormatter.ISO_LOCAL_DATE_TIME // This standard ISO 8601 format is close to your desired output.
) // Returns a `String`.
.replace( "T" , " " ) // Replace `T` in middle with a SPACE.
现代方法使用了几年前的 java.time 类,这些类取代了可怕的旧日期时间类,例如Calendar
和SimpleDateFormat
。
想要当前的日期和时间
使用Instant
以UTC捕获当前时刻。
Instant instant = Instant.now() ;
要通过特定区域(时区)的人们所使用的挂钟时间查看同一时刻,请应用ZoneId
以获得ZonedDateTime
。
以continent/region
的格式指定proper time zone name,例如America/Montreal
,Africa/Casablanca
或Pacific/Auckland
。切勿使用3-4个字母的缩写,例如EST
或IST
,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
或者,作为一种快捷方式,将ZoneId
传递给ZonedDateTime.now
方法。
ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( "Pacific/Auckland" ) ) ;
java.time 类使用nanoseconds的分辨率。这意味着一秒钟的小数点后最多9位数字。如果只需要三毫秒,truncate。将所需的限制作为ChronoUnit
枚举对象传递。
ZonedDateTime
.now(
ZoneId.of( "Pacific/Auckland" )
)
.truncatedTo(
ChronoUnit.MILLIS
)
“ dd / MM / yyyy HH:mm:ss.SS”格式
我建议在生成字符串时始终包括UTC偏移量或时区,以避免产生歧义和误解。
但是,如果您坚持要这样做,则可以在生成表示日期时间值的字符串时指定特定格式。内置的预定义格式化程序几乎可以满足您所需的格式,但是对于T
来说,您需要一个SPACE。
String output =
zdt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME )
.replace( "T" , " " )
;
sdf1.applyPattern(“ dd / MM / yyyy HH:mm:ss.SS”);
日期= sdf1.parse(strDate);
永远不要使用用于呈现给人类的文本来交换日期时间值。
相反,请使用为此目的定义的标准格式,该格式可以在ISO 8601中找到。
java.time 在解析/生成字符串时默认使用这些ISO 8601格式。
在交换特定时刻时,始终包括UTC偏移量或时区的指示符。因此,应避免上面讨论的所需格式进行数据交换。而且,一般最好与UTC交流一下。这表示 java.time 中的Instant
。您可以从Instant
交换ZonedDateTime
,从而在同一时刻,时间轴上的同一点,但不同的时钟时间有效地从时区调整为UTC。
Instant instant = zdt.toInstant() ;
String exchangeThisString = instant.toString() ;
2018-01-23T01:23:45.123456789Z
此ISO 8601格式在末尾使用Z
表示UTC,发音为“ Zulu”。
java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.Date
,Calendar
和SimpleDateFormat
。
目前位于Joda-Time的maintenance mode项目建议迁移到java.time类。
要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310。
您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*
类。
在哪里获取java.time类?
ThreeTen-Extra项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如Interval
,YearWeek
,YearQuarter
和more。
答案 6 :(得分:1)
这是一个使用Java 8并使用“新”日期和时间API LocalDateTime的简单代码段:
def on_click(self):
item = self.tableWidget.selectedItems()
print (item[0].text())
答案 7 :(得分:0)
免责声明:此答案 not 并不支持Date
类的使用(实际上,该类早已过时且设计欠佳,因此我不希望完全使用它)。我尝试用格式回答有关日期和时间对象的定期重复问题。为此,我以Date
类为例。其他课程将在最后处理。
您不希望Date
使用特定格式。除了最简单的一次性程序以外,所有其他方法中的良好做法都是将用户界面与模型和业务逻辑分开。 Date
对象的值属于您的模型,因此请将您的Date
放在此处,切勿让用户直接看到它。当您坚持这一点时,Date
所采用的格式将不再重要。只要用户看到日期,就将其格式化为String
并向用户显示字符串。同样,如果您需要特定的格式来保持或与另一个系统交换,则可以将Date
格式化为字符串以达到此目的。如果用户需要输入日期和/或时间,请接受字符串或使用日期选择器或时间选择器。
特殊情况:存储到SQL数据库中。您的数据库似乎需要特定的格式。不是这样使用yourPreparedStatement.setObject(yourParamIndex, yourDateOrTimeObject)
,其中yourDateOrTimeObject
是LocalDate
,Instant
,LocalDateTime
或java.time中适当的日期时间类的实例。同样,不必担心该对象的格式。搜索更多详细信息。
Date
没有,因为不能具有格式。这是一个时间点,仅此而已。一个值的容器。在您的代码sdf1.parse
中,将字符串转换为Date
对象,即一个时间点。它既不保留字符串也不保留字符串中的格式。
要结束这个故事,我们也来看看代码中的下一行:
System.out.println("Current date in Date Format: "+date);
为了执行+
符号所需的字符串连接,Java需要首先将Date
转换为String
。它通过调用toString
对象的Date
方法来实现。 Date.toString
总是产生类似Thu Jan 05 21:10:17 IST 2012
的字符串。您无法更改它(Date
的子类中除外,但是您不希望那样做)。然后,将生成的字符串与字符串文字连接起来,以生成由System.out.println
打印的字符串。
简而言之,“格式”仅适用于日期的字符串表示形式,不适用于日期本身。
我认为我所写的完全符合我们的期望。与其他类型相似。考虑一下int
。相同的int
可以格式化为53,551
,53.551
(带点作为千位分隔符),00053551
,+53 551
甚至{{1} }。所有这些格式都会产生字符串,而0x0000_D12F
保持不变并且不会更改其格式。使用int
对象,它是完全相同的:您可以将其格式化为许多不同的字符串,但是Date
本身始终保持不变。
不,您不能,并且出于与上述相同的原因。上面提到的所有课程,实际上我从未见过的日期或时间课程,都没有一种格式。您只能在日期时间对象之外的Date
中使用所需的格式。
String
更现代,对程序员更友好的类)答案 8 :(得分:0)
如果您使用的是JAVA8 API,则此代码会有所帮助。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateTimeString = LocalDateTime.now().format(formatter);
System.out.println(dateTimeString);
它将以给定的格式打印日期。
但是,如果再次创建LocalDateTime对象,它将在日期和时间之间打印“ T”。
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
System.out.println(dateTime.toString());
因此,正如前面的帖子中所述,表示和用法也有所不同。
最好使用“ yyyy-MM-dd'T'HH:mm:ss”模式并相应地转换字符串/日期对象。
答案 9 :(得分:0)
使用
Date date = new Date();
String strDate = sdf.format(date);
Calendar cal = Calendar.getInstance();
String strDate = sdf.format(cal.getTime());