How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Get time with hour, minute, second, millisecond, microsecond
You won't have microseconds, because a Date stores the number of milliseconds since Jan. 1 1970. For the milliseconds, use S
, as documented in the javadoc.
Why I can't parse this date format yyyy-MM-dd'T'HH:mm:ss.SSSZ?
You should use Z
the same way you use T
for the parser to recognize the character in format
val stringDate = "2021-12-16T16:42:00.000Z"
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
var consultationDate = sdf.parse(stringDate)
Get current time without methods
The LocalDateTime.now() method returns the instance of LocalDateTime class so if you print the instance of LocalDateTime class, it will print the current time and time.
To get it the the right format you need to format the current date using DateTimeFormatter class included in JDK 1.8
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class CurrentDateTime {
public static void main(String[] args) {
DateTimeFormatter date_wanted = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(date_wanted.format(now));
}
}
How can I get the current date and time in UTC or GMT in Java?
java.util.Date
has no specific time zone, although its value is most commonly thought of in relation to UTC. What makes you think it's in local time?
To be precise: the value within a java.util.Date
is the number of milliseconds since the Unix epoch, which occurred at midnight January 1st 1970, UTC. The same epoch could also be described in other time zones, but the traditional description is in terms of UTC. As it's a number of milliseconds since a fixed epoch, the value within java.util.Date
is the same around the world at any particular instant, regardless of local time zone.
I suspect the problem is that you're displaying it via an instance of Calendar which uses the local timezone, or possibly using Date.toString()
which also uses the local timezone, or a SimpleDateFormat
instance, which, by default, also uses local timezone.
If this isn't the problem, please post some sample code.
I would, however, recommend that you use Joda-Time anyway, which offers a much clearer API.
How to generate date with milliseconds
new SimpleDateFormat("dd-MM-yyyy:HH:mm:ss.sss")
You're never printing milliseconds here. Read the javadoc of SimpleDateFormat, or the answers you already got. The pattern for milliseconds is SSS
, not sss
. sss
prints seconds.
Note that even in your incorrect test, you got 10 values being printed. And that is a proof that the 10 dates are different. If they were the same, you'd have a single value printed.
Change date format Java getting Unparseable date Error
Change the format to
SimpleDateFormat dt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date lastUpdatedDateFormatted = dt.parse(lastUpdatedDate);
System.out.println(lastUpdatedDateFormatted);
And it should be able to parse the date then.
Related Topics
Is Method Reference Caching a Good Idea in Java 8
Convert HTML Character Back to Text Using Java Standard Library
Pure Java HTML Viewer/Renderer for Use in a Scrollable Pane
Multiple Line Code Example in Javadoc Comment
How to Escape Special HTML Characters in Jsp
Parse JSON from Httpurlconnection Object
By Default, Jsf Generates Unusable Ids, Which Are Incompatible with the CSS Part of Web Standards
Java Unsupported Major Minor Version 52.0
JSON Java 8 Localdatetime Format in Spring Boot
Explicitly Calling a Default Method in Java
How to Call a Static Method in Jsp/El
How to Read and Copy the Http Servlet Response Output Stream Content for Logging
How to Handle Calendar Timezones Using Java
How to Run .Jar File by Double Click on Windows 7 64-Bit
Verification of Element in Viewport in Selenium