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
Related
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.
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 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.
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.
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.