get today's date and time as string [duplicate] - java

This question already has answers here:
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 6 years ago.
I am new to Java and trying to use Calendar object to get date and time of now as a string. I am particularly stuck at object and object conversions.
Here is the format I need (as a string):
2016-03-30T14:21:00Z
If I could just get the date and time format right, I could play around with the string but I am struggling with deprecated methods.
Thank you for replies

Your best bet is to start using Java 8's new Time API (or JodaTime if you can't use Java 8)
LocalDateTime now = LocalDateTime.now();
String isoFormat = DateTimeFormatter.ISO_INSTANT.format(now.toInstant(ZoneOffset.UTC));
System.out.println(isoFormat);
outputs 2016-03-30T17:51:38.639Z (when I tested it)

Solved my question using this link:
http://beginnersbook.com/2013/05/current-date-time-in-java/
Thanks for replies, I will also look into Java 8' time API

Related

How to format any kind format of Date or DateTime String to one exact format date string in Java? [duplicate]

This question already has answers here:
How to check if string matches date pattern using time API?
(3 answers)
Parse any date in Java
(6 answers)
Format date in yyyy-MM-dd hh:mm:ss format from whatever format
(2 answers)
How to get the given date string format(pattern) in java?
(10 answers)
Closed 1 year ago.
I have several date data in the database with different format like this:
When
---
2021-11-20
2021-11-20 05:03:31
2021-11-20 04:19:25.4
19-11-2021 19:11
19/11/2021 17.39
19 November'21 16:00
19-November-2021
19/11/2021
19-Nov-2021
Now I would like to change them in one go regardless of the formatting it was to dd-mm-yyyy so they can be uniformed in Java. I have found some answers in stack overflow before such as the usage of SimpleDateTime and DateTimeFormatter, but the datetime format were seemed as only one and they wanted to change to one specific datetime form, but in my case, the datetime form seems very random. Any idea would greatly be appreciated. Thanks in advance.
P.S. I would like to sanitize the upcoming inputs from users since it has the input field for "when", but the filling format is still up to user to pick which they like. This is sure not a good practice, but changing this fundamental and most likely used would cause such unnecessary conflict.
I would suggest to create a cutom formatter using pattern and use it. Something like:
public static final MY_DATE_TIME_PARSER = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd[['T']HH[[':']mm[[':'][ss['.'[SSS][SS][S]]]['Z']]]]").parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0).parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0).toFormatter();
And then use it as:
String string = "2021-01-01T10:20:30.Z";
LocalDateTime localDateTime = LocalDateTime.parse(string, MY_DATE_TIME_PARSER);
Try testing it with different inputs and see if it solves your purpose.

How to convert format (2018-04-12T03:00:00.000Z) to Date from java.util package using Java [duplicate]

This question already has answers here:
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Closed 4 years ago.
Hello there im receiveing from frontend a string who has this format (2018-04-12T03:00:00.000Z), so, i have to convert it to Date from Util package using Java.(java.util.Date)
Is there a good way to do this?
If you are using Java 8+, the easiest way is to use the java.time package to parse the date:
Date date = Date.from(Instant.parse("2018-04-12T03:00:00.000Z"));
String sDate1="2018-04-12T03:00:00.000Z";
Date date1=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(sDate1);
System.out.println(sDate1+"\t"+date1);

Transform a String in unix time java android [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
At the moment I have a String with the folowing format :
'DD/MM/YYYY' , and I'm willing to store this in a SQLite database as a date.
Since it is impossible to store it as a datetime, I've decided Integers would be the best choice, and someone told me about the Unix epoch solution. The thing is that I'm very unfamiliar with that, and I can't seem to convert a String into a unix epoch time...
Is there a way to directly convert a String with my format into a unix epoch time, or am I doing it wrong and should I change something?
I've read this question :Unix epoch time to Java Date object But still can not find my way out with my String...
Take a look at Parsing String date to date and adjust the format to yours.
If you have a java.util.Date just use http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime() to get the UNIX timestamp as long.
If in doubt, read the docs! ;)
https://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.parse(<value>);

Netbeans isn't giving the proper date format [duplicate]

This question already has answers here:
java date problem in parsing
(4 answers)
Closed 7 years ago.
I am setting the date in gui using JDateChooser , but it is not taking the proper value . I have used the format
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
I am choosing the date from the calender provided by JDateChooser, but the month value that it is taking finally is out of bounds!!! I have no clue. There is no problem in the calender they have provided. But the value they take after processing is clearly out of bounds.
The jar used by me is : jcalender-1.4.jar
mm is minutes. You want MM. It's in the documentation.
Need to change yyyy.MM.ddon this format . may be it works

How do I create a new Joda DateTime truncated to the last hour? [duplicate]

This question already has answers here:
JodaTime equivalent of DateUtils.truncate()
(4 answers)
Closed 7 years ago.
I am pulling timestamps from a file that I want to create a new DateTime for, but I want to create the DateTime at the floor of the hour (or any Joda Period will do).
How Can I do this?
Wohoo, found it. Simple like everything in Joda once I traced down the calls.
DateTime dt = new DateTime().hourOfDay().roundFloorCopy();

Categories