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
Related
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 5 days ago.
Improve this question
In a data stream, I'm receiving date attribute values in milliseconds in Pacific timezone. Like for date = Wed Jan 18 2023 18:00:00 Pacific Time, I'm receiving milliseconds value as 1674064800000. It is milliseconds since 1/1/1970 0:00 PST (I am 100 % sure).
But I want to store this in UTC. How can 1674064800000 millis in PST be converted to 1674093600000 millis in UTC in java/kotlin?
It's a very strange need, as time since 1970-01-01 is usually epoch-time, which is time zone independant.
However, you can achieve your need by adding your offset to a set time, like this:
System.out.println(LocalDate.EPOCH.atStartOfDay(ZoneId.of("America/Vancouver")).toInstant().toEpochMilli() + millis)
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 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 4 years ago.
Improve this question
I am trying gmt asia/kolkata timemillis convert to utc timemillis but it returns same value. Environment time is asia/kolkata
The epoch is time zone independent. So you should get the same number of milliseconds since the epoch back no matter which time zone you convert to.
So the result you got is correct.
You can try as this
Instant date = Instant.ofEpochMilli(1549362600000l);
LocalDateTime utc = LocalDateTime.ofInstant(date, ZoneOffset.UTC);
You need not to use Joda time api has moved to java 1.8 has implemented the same you can use above same from java.time package
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 4 years ago.
Improve this question
How can I convert the current time in milliseconds, which is a Long, to a date in specific format?
The format that I need is yyyy-MM-dd HH:mm. This should be of type Date, not String.
You are confused. The type Date is a number of milliseconds since January 1 1970 midnight UTC. It has no inherent format. There is a default system format for a Date, but you cannot alter it. You will need to format your Date as a String if you need that particular String format.
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.