Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuxingloh/791bd38c2331bc814c8a to your computer and use it in GitHub Desktop.
Save fuxingloh/791bd38c2331bc814c8a to your computer and use it in GitHub Desktop.
DateUtil From Puffin Core, and stackoverflow Or you could just use DateUtils from apache library, it is much better
// Date Time Utils
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* All date time are default to utc
* Created by Fuxing
* Date: 21/1/2015
* Time: 12:51 AM
* Project: PuffinCore
*/
public final class DateUtil {
private DateUtil() {/* NOT Suppose to init */}
public static final String STRING_DATE_FORMAT = "dd-MM-yyyy hh:mm:ss";
public static final DateFormat DateFormat = new SimpleDateFormat(STRING_DATE_FORMAT);
/**
* @return Current millis
*/
public static long millisNow() {
return System.currentTimeMillis();
}
/**
* @return Current date
*/
public static Date now() {
return new Date(millisNow());
}
public static Instant instanceNow() {
return zonedNow().toInstant();
}
public static ZonedDateTime zonedNow() {
return ZonedDateTime.now(ZoneId.of("UTC"));
}
public static LocalDateTime localNow() {
return zonedNow().toLocalDateTime();
}
public static LocalTime timeNow() {
return zonedNow().toLocalTime();
}
public static ZonedDateTime toZonedDateTime(Date date) {
return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC"));
}
public static Date addYears(Date date, int years){
return add(date, Calendar.YEAR, years);
}
public static Date addMonths(Date date, int months){
return add(date, Calendar.MONTH, months);
}
public static Date addDays(Date date, int days) {
return add(date, Calendar.DATE, days);
}
public static Date addMinutes(Date date, int minutes) {
return add(date, Calendar.MINUTE, minutes);
}
public static Date addSeconds(Date date, int seconds){
return add(date, Calendar.SECOND, seconds);
}
public static Date addMillis(Date date, int millis){
return add(date, Calendar.MILLISECOND, millis);
}
/**
* @param date date to edit
* @param type type of edit
* @param unit units to edit
* @return edited date
*/
public static Date add(Date date, final int type, int unit) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(type, unit);
return cal.getTime();
}
/**
* @param date date to convert to string
* @return string data with standard format
*/
public static String toString(Date date) {
return DateFormat.format(date);
}
/**
* @param date date to convert to string
* @return Singapore string data with standard format
*/
public static String toSgString(Date date) {
return toZonedDateTime(date).format(DateTimeFormatter.ofPattern(STRING_DATE_FORMAT, Locale.ENGLISH).withZone(ZoneId.of("GMT+8")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment