We need milliseconds to construct a DateTime. So take your input of seconds, and multiply by a thousand. Note that the result must be a bit long as we would overflow a bit int.
Feed that count of milliseconds to constructor. That particular constructor assumes the count is from the Unix epoch of So adjust time zone as desired, after construction. Never use 3 or 4 letter codes such as EST as they are neither standardized not unique. Dump to console. Note how the date is different in Montréal, as the new day has begun in Europe but not yet in America.
The makers of Joda-Time have asked us to migrate to its replacement, the java. time framework as soon as is convenient. While Joda-Time continues to be actively supported, all future development will be done on the java. time classes and their extensions in the ThreeTen-Extra project.
The java-time framework is defined by JSR and built into Java 8 and later. An Instant is a moment on the timeline in UTC with a resolution of nanoseconds.
Its epoch is the first moment of in UTC. Apply an offset-from-UTC ZoneOffset to get an OffsetDateTime. Better yet, if known, apply a time zone ZoneId to get a ZonedDateTime. It looks like your longs are seconds, and not milliseconds. Date constructor takes time as millis, so. Those are probably timestamps in seconds and not in milliseconds which is required for the java new Date long constructor.
Just multiply them by and you should be allright. The long values, most likely, correspond to Epoch timestamps, and the values are:. One can convert these long values to java. Date , taking into account the fact java. Date uses millisecs — as previously hinted, but with some flaw - like this:.
Below are the results of displaying the converted long value to java. Date without using and using the DateFormat:. toString returns "Thu Jan 15 CET ". Make sure that your values are not in this situation lower than New Date number returns a date that's number milliseconds after 1 Jan Odds are you date format isn't showing hours, minutes, and seconds for you to see that it's just a little bit after 1 Jan You need to parse the date according to the correct parsing routing.
I don't know what a is, but if it's seconds after 1 JAN , then multiply it to yield milliseconds. If it is not, then convert it in some manner to milliseconds after if you want to continue to use java. Works for me. You probably want to multiplz it with , since what you get are the seconds from and you have to pass the milliseconds from jan 1 Date has constructor new Date Long milliseconds - Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, , GMT.
Stack Overflow for Teams — Start collaborating and sharing organizational knowledge. Create a free Team Why Teams? Learn more about Collectives.
Learn more about Teams. Converting Long to Date in Java returns Ask Question. Asked 10 years, 11 months ago. Modified 7 months ago. Viewed k times. Anyone know another way to convert it correctly? java date long-integer. Improve this question. edited Jan 17, at BalusC 1. asked Sep 20, at mmmiki mmmiki 1, 2 2 gold badges 10 10 silver badges 5 5 bronze badges. Could you tell what values do you expect?
It looks like you are passing 0 to the constructor. Some more code might help. mmmiki - you should accept an answer — Stewart. it returns jan 15th , here, not jan 1st. FYI, the terribly flawed date-time classes such as java. Date , java. Calendar , and java.
SimpleDateFormat are now legacy , supplanted by the java. time classes built into Java 8 and later. Add a comment. Sorted by: Reset to default. Highest score default Trending recent votes count more Date modified newest first Date created oldest first.
Improve this answer. edited May 24, at answered Sep 20, at BalusC BalusC 1. toMillis L ; for a cleaner and less magic-numberesque solution. tl;dr java. Internally, a count of nanoseconds since TZ. Know Your Data People use various precisions in tracking time as a number since an epoch. So when you obtain some numbers to be interpreted as a count since an epoch, you must determine: What epoch? Many epochs dates have been used in various systems.
But you should not assume this epoch. What precision? Are we talking seconds, milliseconds , microseconds , or nanoseconds since the epoch? What time zone? But sometimes, when involving inexperienced or date-time ignorant programmers, there may be an implied time zone. Lessons learned: Determine, don't assume, the meaning of received data. Read the doc. Your Data Your data seems to be in whole seconds.
Joda-Time The java. Note the appended "L". withZone DateTimeZone. dateTimeParis: T time The makers of Joda-Time have asked us to migrate to its replacement, the java. ofInstant instant , zoneId ;. edited Jul 10, at answered Dec 17, at Basil Bourque Basil Bourque k 89 89 gold badges silver badges bronze badges. Home Spring boot Spring boot JMS RabbitMQ Interview questions Jenkins interview questions Spring boot interview questions JSP Interview Questions and Answers Spring MVC Rest Interview Questions J2EE Servlet JSP Core java String File handling Date Time Calendar Collections framework Java Arraylist Example Programs Java HashSet Example Programs Java HashMap Example Programs Java LinkedList Example Programs Java LinkedHashSet Example Program Java LinkedHashMap Example Programs Java ListIterator Example Programs Java TreeSet Example programs Java TreeMap Example Programs String Builder Example Prorgrams.
Servlet Interview Questions Spring Boot File String HashSet ArrayList HashMap String Uncategorized Servlet Interview Questions Spring boot Interview Questions Spring Boot File String HashSet ArrayList HashMap. Related posts:. You may also like. Uncategorized JSP Interview Questions and Answers November 13, Uncategorized Maven tutorial with best practices October 7, Uncategorized JSP Page life Cycle September 19, About the author. View All Posts. Click here to post a comment.
Glad to see this post. I learned lot of things from this post. Java LinkedHashSet Add Example Program.
Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. Reference:- DateFormat and SimpleDateFormat. Here timeStamp is your long integer which is actually timestamp in millieseconds, you get the java date object, now you can convert it into string by this. I am providing the modern answer. I suggest using java.
time, the modern Java date and time API, for your date work. This will work on your Android version:. I recommend that you use a built-in localized date format for presentation to your user.
I took Catalan date format just as an example. Formats for many languages, countries and dialects are built-in. The SimpleDateFormat class used in most of the old answers is a notorious troublemaker of a class. The Date class also used is poorly designed too. Fortunately they are both long outdated. And I just find java.
time so much nicer to work with. time works nicely on both older and newer Android devices. It just requires at least Java 6. Stack Overflow for Teams — Start collaborating and sharing organizational knowledge. Create a free Team Why Teams? Learn more about Collectives. Learn more about Teams. Convert from Long to date format Ask Question. Asked 10 years, 1 month ago.
Modified 1 year, 10 months ago. Viewed 82k times. I have this value in Long format: It is possible to convert it to Date format? android date long-integer. Improve this question. edited Apr 6, at Suragch k gold badges silver badges bronze badges. asked Aug 2, at HaOx HaOx 1, 5 5 gold badges 25 25 silver badges 36 36 bronze badges. Add a comment. Sorted by: Reset to default. Highest score default Trending recent votes count more Date modified newest first Date created oldest first.
You can use below line of code to do this. Here timeInMilliSecond is long value. format new Date TimeinMilliSeccond ; Or you can use below code too also. toString ; Reference:- DateFormat and SimpleDateFormat P.
Change date format according to your need. Improve this answer. edited Mar 24, at Meno Hochschild answered Aug 2, at AAnkit AAnkit 27k 12 12 gold badges 58 58 silver badges 70 70 bronze badges. Just to add, if you are also getting some date from it, please ensure your timestamp is in miliseconds. A datetimestamp of recent years should have 13 digits in it. I was struggling a lot with this as mine had 10 digits. You can use method setTime on the Date instance or the contructor Date long ; setTime long time Sets this Date object to represent a point in time that is time milliseconds after January 1, GMT.
Mark Bakker Mark Bakker 1, 8 8 silver badges 17 17 bronze badges. format dateObj ;. Adeel Pervaiz Adeel Pervaiz 1, 10 10 silver badges 11 11 bronze badges. time and ThreeTenABP I am providing the modern answer. ofLocalizedDate FormatStyle. withLocale Locale. ofEpochMilli millisecondsSinceEpoch. atZone ZoneId. format dateFormatter ; System. time require Android API level 26? In Java 8 and later and on newer Android devices from API level 26 the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes ThreeTen for JSR ; see the links at the bottom. On older Android use the Android edition of ThreeTen Backport.
And make sure you import the date and time classes from org. bp with subpackages. Links Oracle tutorial: Date Time explaining how to use java. Java Specification Request JSR , where java.
time was first described. ThreeTen Backport project , the backport of java. time to Java 6 and 7 ThreeTen for JSR ThreeTenABP , Android edition of ThreeTen Backport Question: How to use ThreeTenABP in Android Project , with a very thorough explanation.
edited Apr 12, at answered Apr 11, at Ole V. format date ;. answered Mar 13, at Bilal Haider Bilal Haider 11 2 2 bronze badges. answered Nov 6, at Jay Patoliya Jay Patoliya 13 3 3 bronze badges.
Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. Not the answer you're looking for? Browse other questions tagged android date long-integer or ask your own question.
The Overflow Blog. Why the number input is the worst input. Hypergrowth headaches Ep. Planned maintenance scheduled for Wednesday, 21 September, UTC Recent Color Contrast Changes and Accessibility Updates.
The [option] tag is being burninated. Should I explain other people's code-only answers? Reviewer overboard! Or a request to improve the onboarding guidance for new Linked
Learn more about Collectives. Make sure that your values are not in this situation lower than fromtimestamp time and ThreeTenABP I am providing the modern answer. Hypergrowth headaches Ep.format currentDate. count. More date related programming examples: What's the current week number? So the solution is to multiply by 1, The makers of Joda-Time have asked us to migrate to its replacement, the java.