怎么比较时间啊。
答案:4 悬赏:0 手机版
解决时间 2021-02-06 15:59
- 提问者网友:眉目添风霜
- 2021-02-05 18:44
怎么比较时间啊。
最佳答案
- 五星知识达人网友:妄饮晩冬酒
- 2021-02-05 19:11
可以用格式化的方法,例如:
SimpleDateTormat sdf=new SimpleDateTormat("YYYY-MM-DD HH:MM:SS");
String timer="2008-08-27 14:07:50:0";
String strResult=sdf.parse(timer);//strResult就是转换后的结果。
你试试吧·
SimpleDateTormat sdf=new SimpleDateTormat("YYYY-MM-DD HH:MM:SS");
String timer="2008-08-27 14:07:50:0";
String strResult=sdf.parse(timer);//strResult就是转换后的结果。
你试试吧·
全部回答
- 1楼网友:怀裏藏嬌
- 2021-02-05 22:05
先转化成格式一样的字符串 然后用字符串比较函数来比较就可以了
- 2楼网友:动情书生
- 2021-02-05 21:45
我给你个类. 将时间转换成long格式如 20080725123011, 然后你再比较. 这样比较的效率会比比较类实例要高
package com.khan.datetime;
import java.util.*;
import java.text.*;
public class SMPTime {
public static long getDateTime() {
Calendar now = null;
long date = 0;
try {
now = Calendar.getInstance();
date = now.get(Calendar.SECOND)
+ 100L * now.get(Calendar.MINUTE)
+ 10000L * now.get(Calendar.HOUR_OF_DAY)
+ 1000000L * now.get(Calendar.DATE)
+ 100000000L * (now.get(Calendar.MONTH) + 1)
+ 10000000000L * now.get(Calendar.YEAR);
} finally {
now = null;
}
return date;
}
public static String getSqlServerDateString(long datetime) {
return getYear(datetime) + "-"
+ getMonth(datetime) + "-"
+ getDay(datetime) + " "
+ getHour(datetime) + ":"
+ getMinute(datetime) + ":"
+ getSecond(datetime);
}
public static long getNow() {
return getDateTime();
}
public static long getDatePart(long time1, long time2) {
Calendar now = null;
Calendar now1 = null;
long time = 0;
try {
now = getCalendar(time1);
now1 = getCalendar(time2);
time = (now.getTimeInMillis() - now1.getTimeInMillis()) / 1000;
} finally {
now = null;
now1 = null;
}
return time;
}
public static long getYearMonth(){
Calendar now = null;
long date = 0;
try {
now = Calendar.getInstance();
date = (now.get(Calendar.MONTH) + 1)
+ 100L * now.get(Calendar.YEAR);
} finally {
now = null;
}
return date;
}
public static long getDay(boolean hour) {
Calendar now = null;
long date = 0;
try {
now = Calendar.getInstance();
date = now.get(Calendar.DATE)
+ 100L * (now.get(Calendar.MONTH) + 1)
+ 10000L * now.get(Calendar.YEAR);
} finally {
now = null;
}
return date;
}
public static long getTime(boolean hour) {
Calendar now = null;
long data = 0;
try {
now = Calendar.getInstance();
data = now.get(Calendar.SECOND)
+ 100L * now.get(Calendar.MINUTE)
+ 10000L * now.get(Calendar.HOUR_OF_DAY);
} finally {
now = null;
}
return data;
}
public static long getDateTime(int year, int month, int day, int hour, int minute, int second) {
return second + 100L * minute + 10000L * hour + 1000000L * day + 100000000L * month + 10000000000L * year;
}
public static long getDateTime(Calendar date, boolean year) {
int nYear = 0;
if (year == true) {
nYear = date.get(Calendar.YEAR);
}
return getDateTime(nYear, date.get(Calendar.MONTH) + 1, date.get(Calendar.DATE), date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE), date.get(Calendar.SECOND));
}
public static long getDateTime(long timeInSecond) {
Calendar date = null;
long d = 0;
try{
date = Calendar.getInstance();
date.setTimeInMillis(timeInSecond * 1000);
d = getDateTime(date, true);
}finally{
date = null;
}
return d;
}
public static long getDateTime(boolean year) {
Calendar rightNow = null;
long d = 0;
try{
rightNow = Calendar.getInstance();
d = getDateTime(rightNow, year);
}finally{
rightNow = null;
}
return d;
}
public static Calendar getCalendar(long time) {
Calendar now = Calendar.getInstance();
now.set(getYear(time), getMonth(time), getDay(time), getHour(time), getMinute(time), getSecond(time));
return now;
}
public static long getMonthStart(boolean year) {
return getMonthStart(Calendar.getInstance(), year);
}
public static long getMonthStart(Calendar now, boolean year) {
int nYear = 0;
if (year == true) {
nYear = now.get(Calendar.YEAR);
}
return getDateTime(nYear, now.get(Calendar.MONTH) + 1, 0, 0, 0, 0);
}
public static int getYear(long date) {
return (int) (date / 10000000000L);
}
public static int getMonth(long date) {
return (int) ((date / 100000000L) % 100);
}
public static int getDay(long date) {
return (int) ((date / 1000000L) % 100);
}
public static int getHour(long date) {
return (int) ((date / 10000L) % 100);
}
public static int getMinute(long date) {
return (int) ((date / 100L) % 100);
}
public static int getSecond(long date) {
return (int) (date % 100);
}
public static int getSecond() {
Calendar now = Calendar.getInstance();
return now.get(Calendar.SECOND);
}
public static int getAge(long birth, long time) {
return getYear(time - birth);
}
public static long getNextDateTime() {
Calendar now = null;
long d = 0;
try{
now = Calendar.getInstance();
now.add(Calendar.DATE, 1);
d = now.get(Calendar.SECOND)
+ 100L * now.get(Calendar.MINUTE)
+ 10000L * now.get(Calendar.HOUR_OF_DAY)
+ 1000000L * now.get(Calendar.DATE)
+ 100000000L * (now.get(Calendar.MONTH) + 1)
+ 10000000000L * now.get(Calendar.YEAR);
}finally{
now = null;
}
return d;
}
public static long getNextDay() {
Calendar now = null;
long d = 0;
try{
now = Calendar.getInstance();
now.add(Calendar.DATE, 1);
d = now.get(Calendar.DATE)
+ 100L * (now.get(Calendar.MONTH) + 1)
+ 10000L * now.get(Calendar.YEAR);
}finally{
now = null;
}
return d;
}
public static long getDay() {
Calendar now = null;
long d = 0;
try{
now = Calendar.getInstance();
d = now.get(Calendar.DATE)
+ 100L * (now.get(Calendar.MONTH) + 1)
+ 10000L * now.get(Calendar.YEAR);
}finally{
now = null;
}
return d;
}
public static boolean isValidDate(String sDate){
try{
return DateFormat.getDateInstance().parse(sDate)!=null;
}catch(ParseException e){
return false;
}
}
public static Date str2Date(String sDate){
try{
return DateFormat.getDateInstance().parse(sDate);
}catch(ParseException e){
return null;
}
}
public static long getBirth(int age, long birth, long time) {
return birth + 10000000000L * (getAge(birth, time) - age);
}
public static void main(String[] args) { //test
System.out.println(getDatePart(20061206020701L, 20061206020638L));
}
}
package com.khan.datetime;
import java.util.*;
import java.text.*;
public class SMPTime {
public static long getDateTime() {
Calendar now = null;
long date = 0;
try {
now = Calendar.getInstance();
date = now.get(Calendar.SECOND)
+ 100L * now.get(Calendar.MINUTE)
+ 10000L * now.get(Calendar.HOUR_OF_DAY)
+ 1000000L * now.get(Calendar.DATE)
+ 100000000L * (now.get(Calendar.MONTH) + 1)
+ 10000000000L * now.get(Calendar.YEAR);
} finally {
now = null;
}
return date;
}
public static String getSqlServerDateString(long datetime) {
return getYear(datetime) + "-"
+ getMonth(datetime) + "-"
+ getDay(datetime) + " "
+ getHour(datetime) + ":"
+ getMinute(datetime) + ":"
+ getSecond(datetime);
}
public static long getNow() {
return getDateTime();
}
public static long getDatePart(long time1, long time2) {
Calendar now = null;
Calendar now1 = null;
long time = 0;
try {
now = getCalendar(time1);
now1 = getCalendar(time2);
time = (now.getTimeInMillis() - now1.getTimeInMillis()) / 1000;
} finally {
now = null;
now1 = null;
}
return time;
}
public static long getYearMonth(){
Calendar now = null;
long date = 0;
try {
now = Calendar.getInstance();
date = (now.get(Calendar.MONTH) + 1)
+ 100L * now.get(Calendar.YEAR);
} finally {
now = null;
}
return date;
}
public static long getDay(boolean hour) {
Calendar now = null;
long date = 0;
try {
now = Calendar.getInstance();
date = now.get(Calendar.DATE)
+ 100L * (now.get(Calendar.MONTH) + 1)
+ 10000L * now.get(Calendar.YEAR);
} finally {
now = null;
}
return date;
}
public static long getTime(boolean hour) {
Calendar now = null;
long data = 0;
try {
now = Calendar.getInstance();
data = now.get(Calendar.SECOND)
+ 100L * now.get(Calendar.MINUTE)
+ 10000L * now.get(Calendar.HOUR_OF_DAY);
} finally {
now = null;
}
return data;
}
public static long getDateTime(int year, int month, int day, int hour, int minute, int second) {
return second + 100L * minute + 10000L * hour + 1000000L * day + 100000000L * month + 10000000000L * year;
}
public static long getDateTime(Calendar date, boolean year) {
int nYear = 0;
if (year == true) {
nYear = date.get(Calendar.YEAR);
}
return getDateTime(nYear, date.get(Calendar.MONTH) + 1, date.get(Calendar.DATE), date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE), date.get(Calendar.SECOND));
}
public static long getDateTime(long timeInSecond) {
Calendar date = null;
long d = 0;
try{
date = Calendar.getInstance();
date.setTimeInMillis(timeInSecond * 1000);
d = getDateTime(date, true);
}finally{
date = null;
}
return d;
}
public static long getDateTime(boolean year) {
Calendar rightNow = null;
long d = 0;
try{
rightNow = Calendar.getInstance();
d = getDateTime(rightNow, year);
}finally{
rightNow = null;
}
return d;
}
public static Calendar getCalendar(long time) {
Calendar now = Calendar.getInstance();
now.set(getYear(time), getMonth(time), getDay(time), getHour(time), getMinute(time), getSecond(time));
return now;
}
public static long getMonthStart(boolean year) {
return getMonthStart(Calendar.getInstance(), year);
}
public static long getMonthStart(Calendar now, boolean year) {
int nYear = 0;
if (year == true) {
nYear = now.get(Calendar.YEAR);
}
return getDateTime(nYear, now.get(Calendar.MONTH) + 1, 0, 0, 0, 0);
}
public static int getYear(long date) {
return (int) (date / 10000000000L);
}
public static int getMonth(long date) {
return (int) ((date / 100000000L) % 100);
}
public static int getDay(long date) {
return (int) ((date / 1000000L) % 100);
}
public static int getHour(long date) {
return (int) ((date / 10000L) % 100);
}
public static int getMinute(long date) {
return (int) ((date / 100L) % 100);
}
public static int getSecond(long date) {
return (int) (date % 100);
}
public static int getSecond() {
Calendar now = Calendar.getInstance();
return now.get(Calendar.SECOND);
}
public static int getAge(long birth, long time) {
return getYear(time - birth);
}
public static long getNextDateTime() {
Calendar now = null;
long d = 0;
try{
now = Calendar.getInstance();
now.add(Calendar.DATE, 1);
d = now.get(Calendar.SECOND)
+ 100L * now.get(Calendar.MINUTE)
+ 10000L * now.get(Calendar.HOUR_OF_DAY)
+ 1000000L * now.get(Calendar.DATE)
+ 100000000L * (now.get(Calendar.MONTH) + 1)
+ 10000000000L * now.get(Calendar.YEAR);
}finally{
now = null;
}
return d;
}
public static long getNextDay() {
Calendar now = null;
long d = 0;
try{
now = Calendar.getInstance();
now.add(Calendar.DATE, 1);
d = now.get(Calendar.DATE)
+ 100L * (now.get(Calendar.MONTH) + 1)
+ 10000L * now.get(Calendar.YEAR);
}finally{
now = null;
}
return d;
}
public static long getDay() {
Calendar now = null;
long d = 0;
try{
now = Calendar.getInstance();
d = now.get(Calendar.DATE)
+ 100L * (now.get(Calendar.MONTH) + 1)
+ 10000L * now.get(Calendar.YEAR);
}finally{
now = null;
}
return d;
}
public static boolean isValidDate(String sDate){
try{
return DateFormat.getDateInstance().parse(sDate)!=null;
}catch(ParseException e){
return false;
}
}
public static Date str2Date(String sDate){
try{
return DateFormat.getDateInstance().parse(sDate);
}catch(ParseException e){
return null;
}
}
public static long getBirth(int age, long birth, long time) {
return birth + 10000000000L * (getAge(birth, time) - age);
}
public static void main(String[] args) { //test
System.out.println(getDatePart(20061206020701L, 20061206020638L));
}
}
- 3楼网友:琴狂剑也妄
- 2021-02-05 20:13
精确点的话就比他们的毫秒数:
//当前时间
Date d1 = new Date();
//数据库取出时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S");
Date d2 = sdf.parse("2008-08-27 14:07:50:0");
//判断结果的大小就行了
d1.getTime() - d2.getTime();
还有方法:
d1.after(d2);//如果d1 在d2 之后就返回 true
d1.before(d2);//刚好跟上边相反
补充:
JSP 页面的话 直接用 java 代码不久行了嘛! 和在类里一样呀!!
<%XXXXX%>
//当前时间
Date d1 = new Date();
//数据库取出时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S");
Date d2 = sdf.parse("2008-08-27 14:07:50:0");
//判断结果的大小就行了
d1.getTime() - d2.getTime();
还有方法:
d1.after(d2);//如果d1 在d2 之后就返回 true
d1.before(d2);//刚好跟上边相反
补充:
JSP 页面的话 直接用 java 代码不久行了嘛! 和在类里一样呀!!
<%XXXXX%>
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯