gpt4 book ai didi

java - 如何将日期(yyyy-mm-dd)转换为int变量?

转载 作者:行者123 更新时间:2023-12-02 03:08:21 26 4
gpt4 key购买 nike

我想将我的公式的日期(在android studio中的java代码中)更改为另一个日期,但我需要int变量中的日期(yyyy-mm-dd)才能使用我的公式(f.example我想要一个=yyyy/2 ,b= mm+3,c= dd-25,新日期的结果为:aaaa/bb/cc),帮我解决这个问题吗?

这些数据用于我的图表。

这是我的java代码:

// generate Dates
Calendar calendar = Calendar.getInstance();
Date d1 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d2 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d3 = calendar.getTime();

GraphView graph = (GraphView) findViewById(R.id.graph);

// you can directly pass Date objects to DataPoint-Constructor
// this will convert the Date to double via Date#getTime()
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(d1, 1),
new DataPoint(d2, 5),
new DataPoint(d3, 3)
});

graph.addSeries(series);

// set date label formatter
graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity()));
graph.getGridLabelRenderer().setNumHorizontalLabels(3); // only 4 because of the space

// set manual x bounds to have nice steps
graph.getViewport().setMinX(d1.getTime());
graph.getViewport().setMaxX(d3.getTime());
graph.getViewport().setXAxisBoundsManual(true);

// as we use dates as labels, the human rounding to nice readable numbers
// is not necessary
graph.getGridLabelRenderer().setHumanRounding(false);

最佳答案

也许我没明白你的问题,但也许是的。

我认为你想要的是获取给定日期的年、月、日的int值。

如果没有,请告诉我并澄清您的问题。

无论如何,这就是如何做到这一点。

// first initialize a Calendar item
Calendar currentDate = Calendar.getInstance();
// this is the current date, you can also set up a Calendar with a custom date and time.
// now get the values:
int years = currentDate.get(Calendar.YEAR);
int months = currentDate.get(Calendar.MONTH);
int days = currentDate.get(Calendar.DAY_OF_MONTH);

您还可以convert a Date object to a Calendar object

引用

关于java - 如何将日期(yyyy-mm-dd)转换为int变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41427328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com