Tutorials Java How to convert UNIX date to String

How to convert UNIX date to String

In this post we will see how to convert unix date to human readable format, how to use SimpleDateFormat class for date conversion.

  • System.currentTimeMillis()  will give date/time as a single numeric value, expressed as the number of milliseconds after the UNIX epoch. Unix epoch is always an long value. You can convert this time to get your time as per your local time zone.
  • You can use util.Date class object to get the date. Using this you can extract the year, month and day value numerically.
  • Calendar.getInstance() gives you a Calendar object initialized with the current date / time, using the default Locale and TimeZone. You can overload the time zone and locale to get specific time zone time.
  • SimpleDateFormat class is used for parsing and formatting date as text string. We just have to specify the pattern for parsing.

How to get current date

DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
//getting current date using util.Date class
Date date = new Date();
System.out.println("Current Date:" + dateFormat.format(date));

//or use calendar instance
//Calendar cal = Calendar.getInstance();
//return dateFormat.format(cal.getTime());

 

Convert UNIX date to String

long unixdate = 1389705117;
DateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(unixdate * 1000);
System.out.println("Formatted Date:" + formatter.format(calendar.getTime()));
Total
0
Shares
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
SteveMoretz
SteveMoretz
3 years ago

Your website is great I’m wondering why it’s not so famous

Previous Post

Anatomy of Xamarin.Android Application

Next Post
Android Tutorials

Edittext Validation in Android Example

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
1
0
Would love your thoughts, please comment.x
()
x