1. 程式人生 > 資訊 >《艾爾登法環》玩家開始偷偷買賣遊戲幣,但有被封號的風險

《艾爾登法環》玩家開始偷偷買賣遊戲幣,但有被封號的風險

package com.houserent.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* 日期工具類
*/
public class DateUtil {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse("2012-09-08 10:10:10");
Date d2 = sdf.parse("2012-09-15 00:00:00");
System.out.println(daysBetween(d1, d2));

// System.out.println(daysBetween("2012-09-08 10:10:10", "2012-09-15 00:00:00"));
}

/**
* 計算兩個日期之間相差的天數
*
* @param smdate 較小的時間
* @param bdate 較大的時間
* @return 相差天數
* @throws ParseException
*/
public static int daysBetween(Date smdate, Date bdate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);

return Integer.parseInt(String.valueOf(between_days));
}

/**
* 字串的日期格式的計算
*/
public static int daysBetween(String smdate, String bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);

return Integer.parseInt(String.valueOf(between_days));
}

}