在Android中获取当前日期的4种方法

In this tutorial I will show you 4 different ways to get current date in android.

在本教程中,我将向您展示4种不同的方法来获取android中的当前日期。

For doing this we will use Calendar, Date, SimpleDateFormate and System class.

为此,我们将使用Calendar,Date,SimpleDateFormate和System类。

如何在Android中获取当前日期? (How to Get Current Date in Android?)

Method 1:

方法1:

In this method we will use Calendar and SimpleDateFormate class.

在此方法中,我们将使用Calendar和SimpleDateFormate类。

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
String date = sdf.format(c.getTime());

Method 2:

方法2:

We can use Calendar class to get current date in another way.

我们可以使用Calendar类以另一种方式获取当前日期。

Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String date = day + "/" + (month+1) + "/" + year;

Method 3:

方法3:

In this method we will use Date and SimpleDateFormate class.

在此方法中,我们将使用Date和SimpleDateFormate类。

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());

Method 4:

方法4:

Another method in which we will use System and SimpleDateFormate class to get current date

我们将使用System和SimpleDateFormate类获取当前日期的另一种方法

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(System.currentTimeMillis());

Comment below if you know any other way to get current date in android.

如果您知道在Android中获取当前日期的任何其他方法,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/10/get-current-date-in-android.html

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值