2

我遵循getDiffDateMap 了计算 2 个日期和整数返回之间Map的差异的方法,这些整数分别代表毫秒、秒、分钟、小时、天、月和年。

public static Map<Integer, String> getDiffDateMap(String dateA, String dateB) {

    Calendar cal = Calendar.getInstance();      
    Map<Integer,String> out = new LinkedHashMap<Integer, String>();
    long timeInMillA = 0;
    long timeInMillB = 0;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

    Date convertedDateA;
    Date convertedDateB;



    try {
        convertedDateA = dateFormat.parse(dateA);           
        cal.setTime(convertedDateA);
        timeInMillA = cal.getTimeInMillis();


        convertedDateB = dateFormat.parse(dateB);           
        cal.setTime(convertedDateB);
        timeInMillB = cal.getTimeInMillis();

    } catch (ParseException e) {
        e.printStackTrace();
    } 

    long mili = timeInMillB - timeInMillA;
    long sec = mili/1000;
    long min = sec/60;
    long hour = min/60;
    long day = hour/24;
    long week = day/7;
    long month = day/31; // ????
    long year = month/12;

    out.put(7, mili + "");
    out.put(6, sec + "");
    out.put(5, min + "");
    out.put(4, hour + "");
    out.put(3, day + "");
    out.put(2, week + "");
    out.put(1, month + "");
    out.put(0, year + "");

    return out;
}

我的问题是根据实际天数计算月份:

long month = day/31; // or 30

例如:

Map<Integer,String> out = getDiffInMillsec("2012-9-01 20:9:01", "2012-10-01 20:10:01");

    System.out.println(Arrays.asList(out)); 

我得到输出:月数和它的 0[{7=2592060000, 6=2592060, 5=43201, 4=720, 3=30, 2=4, 1=0, 0=0}] 在哪里1。因为差异只有 30 天。我需要添加什么流程来解决这个问题?有什么建议么?

4

5 回答 5

6

我遵循方法 getDiffDateMap 计算两个日期之间的差异并返回整数映射,分别表示毫秒、秒、分钟、小时、天、月和年。

不要重新发明轮子:)

Joda Time有代码可以完成所有这些以及更多工作。例如:

LocalDateTime start = ...;
LocalDateTime end = ...;
Period difference = new Period(start, end, PeriodType.yearMonthDayTime());
int months = difference.getMonths(); // etc

请注意,当您刚刚将差异转换为毫秒数时,您无法获得月数 - 因为月数将取决于开始/结束日期。(例如,30 天可能是也可能不是一个月……)

我强烈建议您在整个 Java 代码中使用 Joda Time,而不是java.util.*. 这是一个更好的API,并且希望这意味着您很少需要编写自己的日期/时间处理代码。

于 2012-11-01T20:32:10.323 回答
4

我建议使用JodaTime#Months

这具有以下功能:

    static Months   monthsBetween(ReadableInstant start, ReadableInstant end) 

创建一个表示两个指定日期时间之间的整月数的 Months。

    static Months   monthsBetween(ReadablePartial start, ReadablePartial end) 

创建一个 Months,表示两个指定的部分日期时间之间的整月数。

于 2012-11-01T20:37:36.970 回答
1

我只想在 Jon Skeet 的帮助下总结我们之前谈过的所有内容,这是一个答案,我使用了JodaTime每个new Period日期值:

import org.joda.time.LocalDateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;

....
    public static Map<Integer, String> getDateTimeDiffMap(String dateA, String dateB) {

    Calendar cal = Calendar.getInstance();
    Map<Integer,String> out = new LinkedHashMap<Integer, String>();

    long timeInMillA = 0;
    long timeInMillB = 0;

    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 

    Date convertedDateA;
    Date convertedDateB;

    try {
    convertedDateA = dateFormat.parse(dateA);           
    cal.setTime(convertedDateA);
    timeInMillA = cal.getTimeInMillis();


    convertedDateB = dateFormat.parse(dateB);           
    cal.setTime(convertedDateB);
    timeInMillB = cal.getTimeInMillis();

} catch (ParseException e) {
    e.printStackTrace();
} 


    LocalDateTime startA = new LocalDateTime(timeInMillA);
    LocalDateTime startB = new LocalDateTime(timeInMillB);

    Period difference = new Period(startA, startB, PeriodType.days());
    int day = difference.getDays();

    difference = new Period(startA, startB, PeriodType.months());
    int month = difference.getMonths();

    difference = new Period(startA, startB, PeriodType.years());
    int year = difference.getYears();

    difference = new Period(startA, startB, PeriodType.weeks());
    int week = difference.getWeeks();

    difference = new Period(startA, startB, PeriodType.hours());
    int hour = difference.getHours();

    difference = new Period(startA, startB, PeriodType.minutes());
    long min = difference.getMinutes();

    difference = new Period(startA, startB, PeriodType.seconds());
    long sec = difference.getSeconds();

    //difference = new Period(startA, startB, PeriodType.millis());
    long mili = timeInMillB - timeInMillA;  


    out.put(7, mili + "");
    out.put(6, sec + "");
    out.put(5, min + "");
    out.put(4, hour + "");
    out.put(3, day + "");
    out.put(2, week + "");
    out.put(1, month + "");
    out.put(0, year + "");      

    return out;
}

例如对于“01-09-2012 20:9:01”、“01-10-2012 20:9:01”,我得到输出:

year=0;
month = 1;
day=30;
hour=720;
...
于 2012-11-03T18:34:45.743 回答
1

java.time

java.util日期时间 API 及其格式化 API已SimpleDateFormat过时且容易出错。建议完全停止使用它们并切换到现代 Date-Time API *

另外,下面引用的是来自Joda-Time主页的通知:

请注意,从 Java SE 8 开始,用户被要求迁移到 java.time (JSR-310) - JDK 的核心部分,它取代了这个项目。

使用java.time现代日期时间 API 的解决方案:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getDiffDateMap("2012-9-01 20:9:01", "2012-10-01 20:10:01"));
    }

    public static Map<Integer, String> getDiffDateMap(String dateA, String dateB) {
        Map<Integer, String> out = new LinkedHashMap<Integer, String>();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s", Locale.ENGLISH);
        LocalDateTime ldtA = LocalDateTime.parse(dateA, dtf);
        LocalDateTime ldtB = LocalDateTime.parse(dateB, dtf);

        out.put(7, String.valueOf(ChronoUnit.MILLIS.between(ldtA, ldtB)));
        out.put(6, String.valueOf(ChronoUnit.SECONDS.between(ldtA, ldtB)));
        out.put(5, String.valueOf(ChronoUnit.MINUTES.between(ldtA, ldtB)));
        out.put(4, String.valueOf(ChronoUnit.HOURS.between(ldtA, ldtB)));
        out.put(3, String.valueOf(ChronoUnit.DAYS.between(ldtA, ldtB)));
        out.put(2, String.valueOf(ChronoUnit.WEEKS.between(ldtA, ldtB)));
        out.put(1, String.valueOf(ChronoUnit.MONTHS.between(ldtA, ldtB)));
        out.put(0, String.valueOf(ChronoUnit.YEARS.between(ldtA, ldtB)));

        return out;
    }
}

输出:

{7=2592060000, 6=2592060, 5=43201, 4=720, 3=30, 2=4, 1=1, 0=0}

ONLINE DEMO

从Trail: Date Time了解有关现代日期时间 API 的更多信息。


* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和 Android API 工作level 仍然不符合 Java-8,请检查Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

于 2021-07-11T08:54:19.563 回答
0
    try {
        String user = request.getParameter("uname");
        out.println(user); 
        String pass = request.getParameter("pass");
        out.println(pass);
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());

        Class.forName( "com.mysql.jdbc.Driver" );
        Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/trans","root","root" ) ;
        Statement st = conn.createStatement(); 

        String sql = "insert into purch (cd,cust,dateof) values('" + user + "','" + pass + "', '" + sqlDate + "')";
        st.executeUpdate(sql);

        Date date = new Date();
        String modifiedDate= new SimpleDateFormat("-MM-").format(date);
        String dd = modifiedDate.toString();
        String da = "ai";
        out.println(dd);

        PreparedStatement statement = conn.prepareStatement("select * from purch where  dateof LIKE ? and cd = ?");    
        statement.setString(1,"%" + dd + "%");
        statement.setString(2,"" + da + "");
        ResultSet rs = statement.executeQuery();


        if(rs != null) {
        while(rs.next()) {
            out.println(rs.getString("cd"));
            out.println(rs.getString("cust"));
            out.println(rs.getString("dateof"));
        }
        }  
    }catch(Exception e) {
        System.out.println(e.toString());
    } 
于 2014-10-16T03:08:01.370 回答