Java Utililty Methods Unix Date

List of utility methods to do Unix Date

Description

The list of methods to do Unix Date are organized into topic(s).

Method

longtoUnixTime(Date date)
to Unix Time
return date.getTime() / 1000;
longtoUnixTime(SimpleDateFormat simpleDateFormat, String audioboxDate)
Given a string representing an UTC date provided by AudioBox server, this method returns a unix timestamp (always in UTC).
simpleDateFormat.applyPattern(AUDIOBOX_DATE_FORMAT);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = simpleDateFormat.parse(audioboxDate);
return date.getTime();
longunixTime()
unix Time
java.util.Calendar cal1 = new GregorianCalendar(1970, 0, 1, 0, 0, 0);
java.util.Calendar cal = java.util.Calendar.getInstance(java.util.Locale.CHINA);
int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
long now = cal1.getTimeInMillis() / 1000;
long now1 = cal.getTimeInMillis() / 1000;
return now1 - now;
...
DateunixTimeToDate(long unixTime)
unix Time To Date
Date date = new Date(unixTime * 1000L); 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
return date;
IntegerunixTimeToDateInt(long unixTime)
unix Time To Date Int
return Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date(unixTime)));
StringunixTimeToString(int time)
unix Time To String
long timeMs = time * 1000L;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = new Date(timeMs);
return format.format(date);