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.
Related
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
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
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a query with the following data
85343
125242
65252
125655
185658
235758
Those are 24 hour times without the format. I tried parsing it with HHmmss to then give it the format hh:mm:ss a it kind of worked with the hours 12+ but with 12- it went to hell giving wrong times. I worked it out with the following code:
DateFormat dateFormat2 = new SimpleDateFormat("Hmmss");
DateFormat dateFormat21 = new SimpleDateFormat("HHmmss");
DateFormat dateFormat5 = new SimpleDateFormat("hh:mm:ss a");
System.out.println((Query.getString(1).length() == 5) ?
dateFormat5.format(dateFormat2.parse(Query.getString(1)))
: dateFormat5.format(dateFormat21.parse(Query.getString(1))));
it solves the problem with the time errors as I get the correct times
8:53:43AM
12:52:42PM
6:52:52 AM
12:56:55 PM
6:56:58 PM
11:57:58PM
Is there anyway to make this better?
I wouldn't use two different DateFormat for parsing but only one :
DateFormat dateFormat21 = new SimpleDateFormat("HHmmss");
DateFormat dateFormat5 = new SimpleDateFormat("hh:mm:ss a");
String s = Query.getString(1);
if (s.length()==5) s = "0"+s;
System.out.println(dateFormat5.format(dateFormat21.parse(s)));
But there's no real problem in your code apart calling getString more than necessary.
Try using some regex like this:
(\d?\d)(\d\d)(\d\d)
Match group 1 is the hours, match group 2 the minute, match group 3 the seconds.
For more about regex in Java see here:
http://www.regular-expressions.info/java.html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
what is the format in SimpleDateFormat for parsing a string ie 07/12/2013 into a date type? I am using the following code, but desired format is not comimg.
SimpleDateFormat formatter ;
Date notBeforeDate ,notAfterDate;
formatter = new SimpleDateFormat("DD/MM/YYYY");
notBeforeDate = (Date)formatter.parse(notBeforeValue);
notAfterDate = (Date)formatter.parse(notAfterValue);
Assuming you are looking for a Java solution. Change the format string :
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse("07/12/2013");
Refer the documentation for the exact format string information.
Try this :
SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy"); ;
Date notBeforeDate ,notAfterDate;
notBeforeDate =formatter.parse("Your String format Date");
notAfterDate =formatter.parse("Your String format Date");
Read More.
Hope it will help you.