How to get a specific date? [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I have the output
Mon Feb 19 02:43:13 EST 2018
Which I've obtained from
System.out.println(new Date());//+ some adjustments with the calendar obviously.
I was curious how I would 'reload' the 'date' from the given output
Mon Feb 19 02:43:13 EST 2018
So if I wanted to refer to the date again when I reload my program, I want it to parse the String and set the date based off of those parameters, is this a good way to handle this?

The best idea I would have is storing the date in a document, I have done this in a similar fashion. I had taken the Unix epoch time, stored the value, then had a converter bring it back to the human readable version. Not too complex.

Related

How to get system date and time format in java? [duplicate]

This question already has answers here:
How can I set date and time formatting in Java that respects the user's OS settings
(7 answers)
How to get the current date/time in Java [duplicate]
(28 answers)
Closed 3 years ago.
I'm trying to read current system date and time format in java, but after analysing i couldn't find any method regarding this, so can anyone help me out.This is what i want read from java.
To get a DateTimeFormatter for the current system locale, you can use DateTimeFormatter.ofLocalizedDateTime()
The FormatStyle parameter controls whether you want long or short representation (Tuesday, April 12, 1952 AD 3:30:42pm PST versus 12.13.52 3:30pm).
A few examples and what I get on my local system:
//Tuesday, 2 April 2019 at 5:49:39 pm Australian Eastern Daylight Time
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).format(ZonedDateTime.now())
//2 April 2019 at 5:50:20 pm AEDT
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).format(ZonedDateTime.now())
//2/4/19, 5:50 pm
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).format(ZonedDateTime.now())
You can ask system current time only in milliseconds. And then you can transform them into DateTime:
long timestamp = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = new Date(timestamp);
System.out.println(dateFormat.format(date));
You can't do it with just the Java API. The system date-time format is operating system specific, and Java has no API to read that (you can read the date-time, and the locale, but not the format). You would have to find some library that does it or write it yourself - but beware that this is going to be vastly different for every OS. E.g. for Windows you might have to read the Windows registry.
To get the locale: Locale.getDefault()
From the locale, you can probably assume the format in most cases (but not if the user chose an untypical format).

DateFormatter parse String to Date gives wrong format [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Change the format of Date Java [closed]
(2 answers)
Closed 4 years ago.
I have the following piece of code:
System.out.println(array[9]);
Date d = df.parse(array[9]);
System.out.println(d.toString());
and the result of this looks like the following:
01.01.2017
Sun Jan 01 00:00:00 CET 2017
My DateFormatter:
DateFormat df = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);
So my question is now why I get the wrong format.
First result is a string, which I must convert to date.
But I got the wrong format, not the German one (dd.MM.yyyy).
What's wrong?
In your example you should use df.format(d) if you plan to convert Date to String. The default Date.toString() method will use the predefined format which you can't control.

SimpleDateFormat not giving correct date [duplicate]

This question already has answers here:
java date format yyyy-mm-dd.hh.MM.ss.ms
(3 answers)
Closed 5 years ago.
I am trying to parse the date from a given String. My input string is 2017-08-11, which represents Aug 11th 2017 (Basically yyyy-mm-dd format)
However if I run
new SimpleDateFormat("yyyy-mm-dd").parse("2017-08-11")
I get Wed Jan 11 00:08:00 GMT 2017
Why is this? I have created a test program at: http://ideone.com/dHe0ZA
M (capital M) represents month and m (small m) represents minute in java.
So the format you have specified is wrong.
Try
new SimpleDateFormat("yyyy-MM-dd").parse("2017-08-11")
You can see more details about the formats here.

Convert string to date along with timezone in java [duplicate]

This question already has answers here:
Generic support for ISO 8601 format in Java 6
(2 answers)
Closed 7 years ago.
I have the following strings which are coming from external server -
"2015-02-25T04:23:34.874-08:00", "2015-02-25T12:22:49.275Z"
I have to show these strings in my site along with the time zone which is available in the above strings.
Following is the format to show the date in my site -
"Feb 25, 2015 03:23 AM, GMT-08:00", "Feb 25, 2015 12:22 AM, GMT"
In JAVA 7 we have new pattern character 'X' to resolve this. We can parse both these values using the single pattern
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
But I am stuck with JAVA 6. What is the right pattern to use for JAVA 6 ?
The incoming date does not depict the timezone, still if you are sure that you would be getting all times in GMT, then you can simply format the incoming date using SimpleDateFormat and get you desired format.
You have an XSD dateTime on your hands which is notoriously hard to parse using standard java (this might have changed in java 8?)
The timezone is not compatible with the SimpleDateFormat parser nor is the random number of milliseconds (can be more than 3 per the spec).
I have not used it but I'm pretty sure Joda time has an easy solution for this. In case you're interested, here is some custom code I wrote a long time ago that parses them as well: https://github.com/nablex/types-base/blob/master/src/main/java/be/nabu/libs/types/utils/TimeFormat.java
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateFormat.format("2015-02-25T04:23:34.874-08:00")
The format is customizable.

Converting long time to a java date [duplicate]

This question already has answers here:
Converting Integer time stamp into java date [duplicate]
(3 answers)
Closed 8 years ago.
I get a date from a request with this format > 1413972425000
When I execute in JavaScript
new Date(1413972425000)
the result is
Wed Oct 22 2014 11:07:05 GMT+0100 (GMT Daylight Time)
So.. What I want is get the datetime but in Java and I don't know how can I do it.
Thanks.
1413972425000 isn't a int value. It is too large for int. You can use it as a long value. 1413972425000L
You can use
Date date=new Date(1413972425000L); // accept long value.
System.out.println(date);
You can use new Date(1413972425000L) to convert long to date. Note the appended L to the numeric value.

Categories

Resources