简体   繁体   English

用 Java 编写更高效

[英]Write more efficiently in Java

can you guys help me to displaying the time difference in Java.你们能帮我显示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输出应该相同,例如:9/24/17 11:31 PM

Sep 24, 2017 2017 年 9 月 24 日

Sep 24, 2017 2017 年 9 月 24 日

Sunday, September 24, 2017 2017 年 9 月 24 日,星期日

September 24, 2017 2017 年 9 月 24 日

9/24/17 17 年 9 月 24 日

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下面的代码几乎与您的代码一样,但通过将其移动到单独的方法来删除逻辑的重复部分。每次您需要使用指定的日期格式打印任何日期时,您都可以通过传递Date对象来调用printDates

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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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