Java' SimpleDateFormat parsing dates incorrectly [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to parse timestamps using the format "yyyymmddHHssmm".
I have two such time stamps:
String timeStamp1 = "20190612221303"//this means 12June2019 10:13:03pm
String timeStamp2 = "20190512222303"//this means 12May2019 10:23:03pm
So I am trying to convert these timestamp string to java date using the following :
Date date1= new SimpleDateFormat("yyyymmddHHssmm").parse(timeStamp1);
Date date2 = new SimpleDateFormat("yyyymmddHHssmm").parse(timeStamp2);
So obviously when I do a
System.out.println(date1.getTime() > date2.getTime());
I would expect the above statement to print true.
But alas it prints false.
Inface the .getTime() of Date prints 1547310793000 for date1 and 1547310803000 for date2, which is obviously incorrect.
Could someone point out what is going on here.

In the format string, you have mm twice: yyyymmddHHssmm. The first occurrence should be MM, for month of year.

What is happening is that you are using
m Minute in hour
And your TimeStamp it is parsing with date
Sat Jan 12 22:03:13 Date1
Sat Jan 12 22:03:23 Date2
You need to use
M Month in year
Check more in the documentation

The format that you have used:yyyymmddHHssmm is ambiguous.
I believe the 5th and 6th characters are used to define months.
Use MM in caps for that.
You have used small mm, which means minutes

Your String passed to SimpleDateFormat should be yyyyMMddHHmmss . Take look here which letter stands for which thing in that formatter. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Related

SimpleDateFormat seems to allow a year of yy when the format is set to dd/mm/yyyy [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
If I have this:
private static final String DATE_FORMAT = "dd/MM/yyyy";
DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
formatter.setLenient(false);
formatter.parse("01/01/98");
Should my application throw an exception if a 2 digit year is passed in? It doesn't seem to have any issue with this.
No. SimpleDateFormat is used for both parsing a Date from a String and generating a String from a Date. The interpretation of the format String varies between these usages. In your case, you are parsing a Date from a String. The 98 is a legitimate value and is interpreted literally (i.e. 98 AD) because you are using yyyy. If you replace the yyyy with yy or y then the parsing should interpret the 98 as 1998. If you want require 4-digit dates then you will need to add some verification code to do that.
Note that setLenient doesn't affect anything here because the values in each position are legitimate. You are not required to have 4 digits in the year position (nor are you limited to 4 digits). If the interpretation is not-lenient and you pass it "1998/04/12" it will throw an exception because 1998 is not in the range of 1-12. If you set lenient then it will mod the value to get it into range (1998 becomes 6) and charge forward. Since the year has no real bounds it has no effect on the year position.

java - convert millisecond to localtime [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to convert millisecond value to local time
For example, we have this value 1601981597562 when I convert it to Date in UTC it gives me the day will be Oct 10 but in local time it should be Oct 11
new DateTime(1601981597562)
Ref
My question whats the way to convert that timestamp to the local time to be Oct 11 instead of Oct 10
You should use date for this .
Add these codes into your activity.
String localTime = String.valueOf(new Date(timeMillis));
You can use Date to convert Epoch to UTC like so:
long epoch = System.currentTimeMillis();
Date date = new Date(epoch);
System.out.println(date);
Ensure that Date is java.util.Date

Read date from webpage to Selenium java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on Selenium Java, I need to get the following date format without the time, as a string in selenium java to validate whether it is up to date with the published date. I used getText() method from the website by splitting from the time and date. Is there any other best ways rather than this solution!
java.time
Edit: I have added more explanation and more code lines.
There’s a little challenge in the fact that the string on the website does not include year. One simple way to handle it is:
ZoneId websiteTimeZone = ZoneId.of("America/Lower_Princes");
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("dd-MMM HH:mm", Locale.ENGLISH);
String stringFromWebsite = "06-Feb 06:37";
MonthDay today = MonthDay.now(websiteTimeZone);
System.out.println("Today is " + today);
MonthDay date = MonthDay.parse(stringFromWebsite, formatter);
System.out.println("Date from website is " + date);
if (date.equals(today)) {
System.out.println("It’s up to date");
} else {
System.out.println("It’s *NOT* up to date");
}
When I ran today (March 12), the snippet printed:
Today is --03-12
Date from website is --02-06
It’s *NOT* up to date
A MonthDay is a month and day of month without year. The advantage of using this class is we don’t need concern ourselves with year. A possible drawback is we can’t compare two such objects determine which one is before or after the other one. Such a comparison would require knowing the year of each one.
We need to know the time zone that the website uses since it is never the same date everywhere on Earth. Please insert the correct one where I put America/Lower_Princes.
I am parsing the string from the website into a MonthDay using a DateTimeFormatter with format pattern dd-MMM HH:mm since lower case d is for day of month, M is for month, H for hour of day and lower case m for minut of the hour. Since I am parsing into a MonthDay, the time is ignored (only its syntax still checked). In the print --03-12 means March 12 and --02-06 similarly February 6 (the date from the website). Since they are not the same, the code prints that the website is not up to date.
A more advanced solution might check if the date is a few days before or after today’s date and/or also look at the time.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Stack Overflow question How do I simply parse a date without a year specified?
You can use selenium's getText(), in order to acquire the value as a String.
Afterwards you can use Java's DateTimeFormatter, to parse this date, and transform it to the format you want

Parse String to Date in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have date like mm-dd-yyy in string format and I am trying to convert the same to Date object in java,
Two ways I tried in
sending the date format as "mm-dd-yyy" which is returning the wrong
date, it always returns month Jan even though the month in the
string is not "01"
sending the date format as "MM-dd-yyy" will return the correct date
as expected.
But I want to understand why the first approach returning wrong?
Can any body tell me the reason.
See the Format
M Month in year
m Minute in hour
Date and time formats are specified by pattern strings. Within the pattern strings, m is interpreted as minute in hour, and M is interpreted as Month in year.
Perhaps you should read JavaDoc API spec.
And you should check that the year of parsed date object is what you intended to. Since your string is MM-dd-yyy, "02-12-014"(which should be 2014-02-12 is actually interpreted as 0014-02-12, which can be not your intended result.

how to convert specified time in particular timezone? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am living in India so my timezone is IST so when I want to fix meeting with the UK client then I just put my time in field like 10 A.M to 11 A.M. Now How to convert this in GMT0000 that is UK time zone?
Basically I have textfied to enter time then after inserted there is one dropdown box that is containing all available timezone then when I select UK timezone then how to convert this process using Java?
Thanks in advance.. tell me to solve my issue..
Note: I don't want to do this manually? cause of selection it will be converted? that is my task.. hope you understand my problem..
Regards..
India time is GMT + 5.30. Then 10.00 -5.30 can consider as GMT time.
In java you can do something like this
DateFormat df = new SimpleDateFormat("HH:mm");
Date dateIST=df.parse("10:00"); // india time
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(df.format(dateIST));// This is UK time
(JAVA answer) In textfield(s) you just need to enter your local time. Take an input from that textfield(s) and pass it in following code.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));//or whatever timezone u want.
String gmtStrDate = simpleDateFormat.format(Calendar.getInstance().getTime());
Or you may find this code suited to your need (your question is unclear):
SimpleDateFormat inputFormat = new SimpleDateFormat("HH:mm");
Date date = inputFormat.parse("time from textfield");//like 11:44
inputFormat.setTimeZone((TimeZone.getTimeZone("IST")));
SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm");
outputFormat.setTimeZone((TimeZone.getTimeZone("GMT")));
String outputText = outputFormat.format(date);
Now outputText is your desired time in UK format.
Note : Here i assumed you are entering time in textfield like 13:44. You can accordingly change that in formatters.

Categories

Resources