简体   繁体   中英

Write more efficiently in Java

can you guys help me to displaying the time difference in Java. I've done it but there's some code that should not have to be rewritten.

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

public class Time {

    public static void main(String[] args) throws Exception {
        Date dt = new Date();
        DateFormat[] dtformat = new DateFormat[6];
        dtformat[0] = DateFormat.getInstance();
        dtformat[1] = DateFormat.getDateInstance();
        dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
        dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
        dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
        dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
        System.out.println("Today :");
        for(DateFormat dateform : dtformat)
            System.out.println(dateform.format(dt));

        String str = "May 21 1980";
        SimpleDateFormat df = new SimpleDateFormat("MMM dd yyyy");
        Date date = df.parse(str);
        long epoch = date.getTime();

        Date dl = new Date(epoch);
        DateFormat[] dlformat = new DateFormat[6];
        dlformat[0] = DateFormat.getInstance();
        dlformat[1] = DateFormat.getDateInstance();
        dlformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
        dlformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
        dlformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
        dlformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
            System.out.println("\nDate of Birth :");
            for(DateFormat dateform : dlformat)
            System.out.println(dateform.format(dl));

            System.out.println("\nDifference Days: " + Diff(dl,dt) + " hari");
    }
    public static long Diff(Date dl, Date dt) {
        long diffdays = dt.getTime() - dl.getTime();
        long days = diffdays / (24 * 60 * 60 * 1000);
        return days;
    }
}

The output should be the same, like : 9/24/17 11:31 PM

Sep 24, 2017

Sep 24, 2017

Sunday, September 24, 2017

September 24, 2017

9/24/17

The below code will pretty much does as your code, but removes the repeated section of the logic by moving it to a separated method.Everytime you need to print any date with the specified Date Formats,You can call printDates by passing a Date Object

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

public class Time {

    public static void main(String[] args) throws Exception {
        Date dt = new Date();
        printDates(dt);

        String str = "May 21 1980";
        SimpleDateFormat df = new SimpleDateFormat("MMM dd yyyy");
        Date date = df.parse(str);
        long epoch = date.getTime();

        Date dl = new Date(epoch);
        printDates(dl);

            System.out.println("\nDifference Days: " + Diff(dl,dt) + " hari");
    }
    public static long Diff(Date dl, Date dt) {
        long diffdays = dt.getTime() - dl.getTime();
        long days = diffdays / (24 * 60 * 60 * 1000);
        return days;
    }


    public static void printDates(Date date){
          DateFormat[] dlformat = new DateFormat[6];
          dlformat[0] = DateFormat.getInstance();
          dlformat[1] = DateFormat.getDateInstance();
          dlformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
          dlformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
          dlformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
          dlformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
          System.out.println("\nDate of Birth :");
          for(DateFormat dateform : dlformat)
          System.out.println(dateform.format(date));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM