在android中获得多年的时差

时间:2015-07-25 20:37:33

标签: java android date

您好我能够获得秒,分钟,小时和天,但是当我尝试多年来问题出现时。

我正在使用以下代码:

        Calendar startDate = new GregorianCalendar(year, month, day);
        Calendar date = Calendar.getInstance();                 
        long diff = date.getTimeInMillis() - startDate.getTimeInMillis();
        long seconds = diff / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long days = hours / 24;
        double years = date.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);

假设开始日期是07/25/1994,结束日期是2015年7月28日 我得到的结果是(21.0)而不是20.97。我需要为用户提供YEARS的确切年龄;你可以帮帮我们吗?谢谢!

3 个答案:

答案 0 :(得分:1)

使用BigDecimal代替doublelong。如果要精确计算浮点数,请始终使用BigDecimal。浮点数学在java中不可靠。简单的例子是System.out.println(2.00 - 1.10);猜猜是什么,它不打印.90

答案 1 :(得分:1)

您需要转换变量:

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

public class Controller {

    @FXML public TableView<Student> studentTable;
    ObservableList<Student> studentArray = FXCollections.observableArrayList();

    //First Name Column
    TableColumn<Student,String> firstNameColumn = new TableColumn<>("First Name");
    firstNameColumn.setMinWidth(50);
    firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));

    //Last Name Column
    TableColumn<Student,String> lastNameColumn = new TableColumn<>("Last Name");
    lastNameColumn.setMinWidth(50);
    lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));

    //Major Column
    TableColumn<Student,String> majorColumn = new TableColumn<>("Major");
    majorColumn.setMinWidth(50);
    majorColumn.setCellValueFactory(new PropertyValueFactory<>("major"));

    //GPA Column
    TableColumn<Student,Double> gpaColumn = new TableColumn<>("GPA");
    gpaColumn.setMinWidth(20);
    gpaColumn.setCellValueFactory(new PropertyValueFactory<>("gpa"));

    //UIN Column
    TableColumn<Student,Integer> uinColumn = new TableColumn<>("UIN");
    uinColumn.setMinWidth(30);
    uinColumn.setCellValueFactory(new PropertyValueFactory<>("uin"));

    //Net ID Column
    TableColumn<Student,Integer> netIDColumn = new TableColumn<>("Net ID");
    netIDColumn.setMinWidth(15);
    netIDColumn.setCellValueFactory(new PropertyValueFactory<>("netID"));

    //Age Column
    TableColumn<Student,Integer> ageColumn = new TableColumn<>("Age");
    ageColumn.setMinWidth(20);
    ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));

    //Gender Column
    TableColumn<Student,String> genderColumn = new TableColumn<>("Gender");
    genderColumn.setMinWidth(50);
    genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));

    studentTable.setItems(studentArray);

    studentTable.getColumns().addAll(firstNameColumn, lastNameColumn, majorColumn, gpaColumn,uinColumn, netIDColumn, ageColumn, genderColumn);

    public void deleteStudent() {
        Student selectedStudent = studentTable.getSelectionModel().getSelectedItem();

答案 2 :(得分:1)

您对经过时间或十进制数计算有疑问吗?

如果是第一个,你应该知道过去几年的十进制数有点奇怪。如果是第二个,您应该更改问题的标题。

经过时间

对于Android,您应该使用Joda-Time库而不是旧的java.util.Date/.Calendar类。老班很出奇。

ISO 8601标准定义了各种日期时间值的字符串格式。对于按年,月,日,小时,分钟和秒计算的时间跨度,格式为PnYnMnDTnHnMnS,其中P定义开头,T分隔日期小时部分。该问题大约为P21Y3D,即21年零3天。 Joda-Time库使用这种标准格式来解析和生成这样的字符串。

LocalDate start = new LocalDate( 1994, 7, 25 ); //  07/25/1994
LocalDate stop = new LocalDate( 2015, 7, 28 );  //  07/28/2015
Period period = new Period( start, stop );

System.out.println("start: " + start + " to stop: " + stop + " is " + period  );

跑步时。

start: 1994-07-25 to stop: 2015-07-28 is P21Y3D

十进制数计算

如果您只是询问十进制数字,那么Samrat Dutta的the answer是正确的:如果您关心准确性,请使用BigDecimal。否则,您正在使用具有浮点计算的基元。 Floating-point 交易执行速度的准确性。作为一般规则,如果对于哪些用于业务问题存在疑问,请使用BigDecimal而不是浮点数。

“年龄的确切年龄”是什么意思?这是最多200个小数位的年数:

  

21.00752908966461327857631759069130732375085557837097878165639972621492128678986995208761122518822724161533196440793976728268309377138945927446954140999315537303216974674880219028062970568104038329911020

Integer days = Days.daysBetween( start, stop ).getDays( );
BigDecimal daysPerYear = new BigDecimal( 365.25 );  // Approximate.
int scale = 200;  // Number of fractional digits desired.
BigDecimal years = new BigDecimal( days ).divide( daysPerYear, scale, RoundingMode.HALF_EVEN );  // Banker's rounding.

System.out.println( "days: " + days + " ÷ " + daysPerYear + " = " + years + " years." );

跑步时。

days: 7673 ÷ 365.25 = 21.00752908966461327857631759069130732375085557837097878165639972621492128678986995208761122518822724161533196440793976728268309377138945927446954140999315537303216974674880219028062970568104038329911020 years.

正如我所说,你可能会发现P21Y3D比这个十进制数更有意义。