📜  Android 中的日期和时间格式

📅  最后修改于: 2022-05-13 01:55:17.145000             🧑  作者: Mango

Android 中的日期和时间格式

Android 中的日期和时间使用Java中的 SimpleDateFormat 库进行格式化,使用 Calendar 实例有助于获取当前系统日期和时间。当前日期和时间属于 Long 类型,可以转换为人类可读的日期和时间。在本文中,讨论了如何将日期和时间值格式化为各种格式并显示出来。查看下图以了解整个讨论。

Android 中的日期和时间格式

想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!

在 Android 中格式化日期和时间的步骤

第 1 步:创建一个空的活动项目

  • 使用 Android Studio 创建一个空的活动项目。参考安卓 |如何在 Android Studio 中创建/启动新项目?

步骤 2:使用 activity_main.xml 文件

  • 包含 8 个 TextViews 的活动文件的主要布局。一种以 Long 类型显示当前系统日期和时间值,另一些以格式化的人类可读方式显示相同的日期和时间值。
  • 要实现 UI,请调用activity_main.xml文件中的以下代码。
XML


  
    
  
    
    
  
    
  
    
    
  
    
  
    
  
    
  
    
  
    
  
    
  


Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import java.text.SimpleDateFormat
import java.util.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        var dateTime: String
        var calendar: Calendar
        var simpleDateFormat: SimpleDateFormat
  
        // register all the text view with appropriate IDs
        val dateTimeInLongTextView: TextView = findViewById(R.id.dateTimeLongValue)
        val format1: TextView = findViewById(R.id.format1)
        val format2: TextView = findViewById(R.id.format2)
        val format3: TextView = findViewById(R.id.format3)
        val format4: TextView = findViewById(R.id.format4)
        val format5: TextView = findViewById(R.id.format5)
        val format6: TextView = findViewById(R.id.format6)
        val format7: TextView = findViewById(R.id.format7)
  
        // get the Long type value of the current system date
        val dateValueInLong: Long = System.currentTimeMillis()
        dateTimeInLongTextView.text = dateValueInLong.toString()
  
        // different format type to format the 
        // current date and time of the system
        // format type 1
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd.MM.yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format1.text = dateTime
  
        // format type 2
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format2.text = dateTime
  
        // format type 3
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd/MM/yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format3.text = dateTime
  
        // format type 4
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd.LLL.yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format4.text = dateTime
  
        // format type 5
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd.LLLL.yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format5.text = dateTime
  
        // format type 6
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("E.LLLL.yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format6.text = dateTime
  
        // format type 7
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("EEEE.LLLL.yyyy KK:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format7.text = dateTime
    }
}


Java
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Calendar;
  
public class MainActivity extends AppCompatActivity {
  
    TextView dateTimeInLongTextView, format1, format2,
             format3, format4, format5, format6, format7;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        String dateTime;
        Calendar calendar;
        SimpleDateFormat simpleDateFormat;
  
        // register all the text view with appropriate IDs
        dateTimeInLongTextView = (TextView) findViewById(R.id.dateTimeLongValue);
        format1 = (TextView) findViewById(R.id.format1);
        format2 = (TextView) findViewById(R.id.format2);
        format3 = (TextView) findViewById(R.id.format3);
        format4 = (TextView) findViewById(R.id.format4);
        format5 = (TextView) findViewById(R.id.format5);
        format6 = (TextView) findViewById(R.id.format6);
        format7 = (TextView) findViewById(R.id.format7);
  
        // get the Long type value of the current system date
        Long dateValueInLong = System.currentTimeMillis();
        dateTimeInLongTextView.setText(dateValueInLong.toString());
  
        // different format type to format the
        // current date and time of the system
        // format type 1
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format1.setText(dateTime);
  
        // format type 2
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format2.setText(dateTime);
  
        // format type 3
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format3.setText(dateTime);
  
        // format type 4
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd.LLL.yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format4.setText(dateTime);
  
        // format type 5
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd.LLLL.yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format5.setText(dateTime);
  
        // format type 6
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("E.LLLL.yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format6.setText(dateTime);
  
        // format type 7
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("EEEE.LLLL.yyyy KK:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format7.setText(dateTime);
    }
}


第 3 步:与 主活动文件

了解Android中使用SimpleDateFormat格式化日期和时间的方式

  • 首先,创建 Calendar 的实例并将要显示的日期和时间的所需格式传递给SimpleDateFormat方法。字符串应包含以下字符,其中一个可能包含分隔符,如 -、/ 等。
  • 下表包括用于生成最常用的日期和时间模式的字符。

Character to be used

Output

ddDate in numeric value
EDay in String (short form. Ex: Mon)
EEEEDay in String (full form. Ex: Monday)
MMMonth in numeric value
yyyyYear in numeric value
LLLMonth in String (short form. Ex: Mar)
LLLLMonth in String (full form. Ex: March)
HHHour in numeric value (24hrs timing format)
KKHour in numeric value (12hrs timing format)
mmMinute in numeric value
ssSeconds in numeric value
aaaDisplays AM or PM (according to 12hrs timing format)
zDisplays the time zone of the region
  • 请参阅以下代码及其输出以更好地了解上表。

科特林

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import java.text.SimpleDateFormat
import java.util.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        var dateTime: String
        var calendar: Calendar
        var simpleDateFormat: SimpleDateFormat
  
        // register all the text view with appropriate IDs
        val dateTimeInLongTextView: TextView = findViewById(R.id.dateTimeLongValue)
        val format1: TextView = findViewById(R.id.format1)
        val format2: TextView = findViewById(R.id.format2)
        val format3: TextView = findViewById(R.id.format3)
        val format4: TextView = findViewById(R.id.format4)
        val format5: TextView = findViewById(R.id.format5)
        val format6: TextView = findViewById(R.id.format6)
        val format7: TextView = findViewById(R.id.format7)
  
        // get the Long type value of the current system date
        val dateValueInLong: Long = System.currentTimeMillis()
        dateTimeInLongTextView.text = dateValueInLong.toString()
  
        // different format type to format the 
        // current date and time of the system
        // format type 1
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd.MM.yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format1.text = dateTime
  
        // format type 2
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format2.text = dateTime
  
        // format type 3
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd/MM/yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format3.text = dateTime
  
        // format type 4
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd.LLL.yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format4.text = dateTime
  
        // format type 5
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("dd.LLLL.yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format5.text = dateTime
  
        // format type 6
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("E.LLLL.yyyy HH:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format6.text = dateTime
  
        // format type 7
        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("EEEE.LLLL.yyyy KK:mm:ss aaa z")
        dateTime = simpleDateFormat.format(calendar.time).toString()
        format7.text = dateTime
    }
}

Java

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Calendar;
  
public class MainActivity extends AppCompatActivity {
  
    TextView dateTimeInLongTextView, format1, format2,
             format3, format4, format5, format6, format7;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        String dateTime;
        Calendar calendar;
        SimpleDateFormat simpleDateFormat;
  
        // register all the text view with appropriate IDs
        dateTimeInLongTextView = (TextView) findViewById(R.id.dateTimeLongValue);
        format1 = (TextView) findViewById(R.id.format1);
        format2 = (TextView) findViewById(R.id.format2);
        format3 = (TextView) findViewById(R.id.format3);
        format4 = (TextView) findViewById(R.id.format4);
        format5 = (TextView) findViewById(R.id.format5);
        format6 = (TextView) findViewById(R.id.format6);
        format7 = (TextView) findViewById(R.id.format7);
  
        // get the Long type value of the current system date
        Long dateValueInLong = System.currentTimeMillis();
        dateTimeInLongTextView.setText(dateValueInLong.toString());
  
        // different format type to format the
        // current date and time of the system
        // format type 1
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format1.setText(dateTime);
  
        // format type 2
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format2.setText(dateTime);
  
        // format type 3
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format3.setText(dateTime);
  
        // format type 4
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd.LLL.yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format4.setText(dateTime);
  
        // format type 5
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd.LLLL.yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format5.setText(dateTime);
  
        // format type 6
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("E.LLLL.yyyy HH:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format6.setText(dateTime);
  
        // format type 7
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("EEEE.LLLL.yyyy KK:mm:ss aaa z");
        dateTime = simpleDateFormat.format(calendar.getTime()).toString();
        format7.setText(dateTime);
    }
}

输出:

Android 输出中的日期和时间格式