Created
February 17, 2013 20:09
-
-
Save zhenyi2697/4973170 to your computer and use it in GitHub Desktop.
Java: Date operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
import java.text.SimpleDateFormat; | |
import java.sql.Timestamp; | |
import java.lang.StringBuffer; | |
public class DateOperation{ | |
private Date date; | |
public DateOperation(){ | |
date = new Date(); | |
} | |
public void printDateAsClass(){ | |
System.out.println(date); | |
} | |
public void printDateAsString(){ | |
SimpleDateFormat yyMMddFormat = new SimpleDateFormat("yyyy-MM-dd"); | |
String yyMMddString = yyMMddFormat.format(date); | |
System.out.println(yyMMddString); | |
SimpleDateFormat hhmmssFormat = new SimpleDateFormat("HH:mm:ss"); | |
String hhmmssString = hhmmssFormat.format(date); | |
System.out.println(hhmmssString); | |
SimpleDateFormat weekInYearFormat = new SimpleDateFormat("w"); | |
String weekInYearString = weekInYearFormat.format(date); | |
System.out.println(weekInYearString); | |
} | |
public void printDateAsTimestamp(){ | |
Timestamp timestamp = new Timestamp(date.getTime()); | |
System.out.println("use java.sql.Timestamp to print as timestamp: "+timestamp.getTime()); | |
System.out.println("or using directly date.getTime(): "+date.getTime()); | |
} | |
public void compareDateUsingMilliSecond(){ | |
Date newDate = new Date(); | |
long dateDifference = newDate.getTime() - date.getTime(); | |
if(dateDifference > 0){ | |
System.out.println("newDate is later..."); | |
}else{ | |
System.out.println("date is later..."); | |
} | |
System.out.println("date difference is :"+dateDifference); | |
} | |
public void convertStringToDate(){ | |
String dateString = "1989-10-23 02:20:34"; | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
Date dateFromString = null; | |
try{ | |
dateFromString = formatter.parse(dateString); | |
}catch(Exception e){} | |
System.out.println("date converted from string: "+dateFromString); | |
} | |
public void convertLongTimestampToDate(){ | |
Date newDate = new Date(); | |
long longTimestamp = newDate.getTime(); | |
longTimestamp += 10000; | |
Date dateFromTimestamp = new Date(longTimestamp); | |
System.out.println("date from long timestamp: "+dateFromTimestamp); | |
} | |
public String compareDateWithCurrentTime(Date date){ | |
Date currentDate = new Date(); | |
StringBuffer sb = new StringBuffer(); | |
sb.append("当前时间和目标时间比"); | |
long timeDiff = currentDate.getTime() - date.getTime(); | |
if(timeDiff > 0){ | |
sb.append("已过"); | |
}else{ | |
sb.append("还差"); | |
timeDiff = timeDiff * -1; | |
} | |
long dayDiff = timeDiff/(1000 * 60 * 60 * 24); | |
long hourDiff = timeDiff/ (1000 * 60 * 60); | |
long minuteDiff = timeDiff/(1000 * 60); | |
long secondDiff = timeDiff/(1000); | |
if(dayDiff != 0){ | |
sb.append(dayDiff + "天"); | |
}else if(hourDiff != 0){ | |
sb.append(hourDiff + "小时"); | |
}else if(minuteDiff != 0){ | |
sb.append(minuteDiff + "分钟"); | |
}else if(secondDiff != 0){ | |
sb.append(secondDiff+"秒"); | |
}else{ | |
sb.append(timeDiff+"毫秒"); | |
} | |
return sb.toString(); | |
} | |
public static void main(String args[]){ | |
DateOperation dateOperation = new DateOperation(); | |
dateOperation.printDateAsClass(); | |
dateOperation.printDateAsString(); | |
dateOperation.printDateAsTimestamp(); | |
dateOperation.compareDateUsingMilliSecond(); | |
dateOperation.convertStringToDate(); | |
dateOperation.convertLongTimestampToDate(); | |
String oldDateString = "2012-08-13"; | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
Date convertedDate = null; | |
try{ | |
convertedDate = sdf.parse(oldDateString); | |
}catch(Exception e){} | |
System.out.println(dateOperation.compareDateWithCurrentTime(convertedDate)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment