Converting Epoch time to date string
Converting Epoch time to date string
I have seen this question asked multiple times and none of the answers seem to be what i need.
I have a long type variable which has an epoch time stored in it.
What i want to do is convert it to a String
for example if the epoch time stored was for today the final string would read:
17/03/2012
How would i to this?
Answer by Jon Skeet for Converting Epoch time to date string
You'd create a Date
from the long
- that's easy:
Date date = new Date(epochTime);
Note that epochTime
here ought to be in milliseconds since the epoch - if you've got seconds since the epoch, multiply by 1000.
Then you'd create a SimpleDateFormat
specifying the relevant pattern, culture and time zone. For example:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US); format.setTimeZone(...);
Then use that to format the date to a string:
String text = format.format(date);
Answer by Reinard for Converting Epoch time to date string
Look into SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); sdf.format(new Date(myTimeAsLong));
Answer by Ajibola for Converting Epoch time to date string
You need to be aware that epoch time in java is in milliseconds, while what you are converting may be in seconds. Ensure that both sides of the conversions are in milliseconds, and then you can fetch the date parameters from the Date object.
Answer by Arunkongara for Converting Epoch time to date string
import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; import java.text.*; public class EPOCH { public static void main(String[] args) throws InterruptedException { try { InputStreamReader isr = new InputStreamReader(System.in); System.out.print("pelase enter Epoch tme stamp : "); BufferedReader br = new BufferedReader(isr); String s=br.readLine(); long l=Long.parseLong(s); l=l*1000; Date date = new Date(l); DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); format.setTimeZone(TimeZone.getTimeZone("Etc/UTC")); String formatted = format.format(date); System.out.println(formatted); format.setTimeZone(TimeZone.getTimeZone("IST")); formatted = format.format(date); Calendar c=Calendar.getInstance(); c.setTime(format.parse(formatted)); formatted = format.format(c.getTime()); System.out.println(formatted); } catch (Exception e) { e.printStackTrace(); } } }
Answer by Basil Bourque for Converting Epoch time to date string
Joda-Time
If by epoch time you meant a count of milliseconds since first moment of 1970 in UTC, then here is some example code using the Joda-Time library?
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); DateTime dateTime = new DateTime( yourMilliseconds, timeZone ); String output = DateTimeFormat.forStyle( "S-" ).withLocale( Locale.CANADA_FRENCH ).print( dateTime );
Other Epochs
That definition of epoch is common because of its use within Unix. But be aware that at least a couple dozen epoch definitions are used by various computer systems.
Answer by Damith Ganegoda for Converting Epoch time to date string
Date date = new Date(String);
this is deprecated.
solution
Date date = new Date(1406178443 * 1000L); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss.SSS"); format.setTimeZone(TimeZone.getTimeZone("Etc/UTC")); String formatted = format.format(date);
make sure multiply by 1000L
Answer by Silambarasan Poonguti for Converting Epoch time to date string
Try this...
sample Epoch timestamp is 1414492391238
Method:
public static String GetHumanReadableDate(long epochSec, String dateFormatStr) { Date date = new Date(epochSec * 1000); SimpleDateFormat format = new SimpleDateFormat(dateFormatStr, Locale.getDefault()); return format.format(date); }
Usability:
long timestamp = Long.parseLong(engTime) / 1000; String engTime_ = GetHumanReadableDate(timestamp, "dd-MM-yyyy HH:mm:ss aa");
Result:
28-10-2014 16:03:11 pm
Happy coding
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment