วิธีการฟอร์แมทอย่างถูกต้องตามวันที่และเวลาที่กำหนดค่าอุปกรณ์เมื่อมีปีเดือนวันชั่วโมงและนาที
วิธีการฟอร์แมทอย่างถูกต้องตามวันที่และเวลาที่กำหนดค่าอุปกรณ์เมื่อมีปีเดือนวันชั่วโมงและนาที
คำตอบ:
ใช้คลาส Java DateFormat มาตรฐาน
ตัวอย่างเช่นในการแสดงวันที่และเวลาปัจจุบันให้ทำดังต่อไปนี้:
Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));คุณสามารถเริ่มต้นวัตถุวันที่ด้วยค่าของคุณเอง แต่คุณควรระวังว่าตัวสร้างได้ถูกคัดค้านและคุณควรใช้วัตถุ Java Calendar จริงๆ
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());วันที่ส่งคืนรูปแบบเป็นประเภทjava.text.DateFormat(และไม่android.text.format.DateFormat)
                    ในความคิดของฉันandroid.text.format.DateFormat.getDateFormat(context)ทำให้ฉันสับสนเพราะวิธีนี้ส่งคืนjava.text.DateFormatมากกว่าandroid.text.format.DateFormat- - "
ดังนั้นฉันใช้รหัสส่วนดังต่อไปนี้เพื่อรับวันที่ / เวลาปัจจุบันในรูปแบบของฉัน
android.text.format.DateFormat df = new android.text.format.DateFormat();
df.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());
or
android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());hh:mm:ssจะให้01:00:00เวลา 13.00 น. คุณจะต้องใช้kk:mm:ssเพื่อรับ13:00:00
                    kคือชั่วโมงในวันที่ (1-24) คุณไม่ได้หมายความว่าHเป็นชั่วโมงในวันที่ (0-23) เช่น HH: mm: ss? ดู: developer.android.com/reference/java/text/SimpleDateFormat.html
                    java.text.SimpleDateFormat(สิ่งที่คุณเชื่อมโยงและใช้Hเป็นเวลาหลายชั่วโมงในช่วง 0-23) และandroid.text.format.DateFormat(สิ่งที่คำตอบเกี่ยวกับและใช้งานkเป็นเวลาหลายชั่วโมงในช่วง 0-23)
                    kอย่างไรก็ตามเอกสารประกอบสำหรับการDateFormatระบุอย่างชัดเจนสำหรับเอกสารประกอบมาตรฐานของรูปแบบสตริงโปรดดู SimpleDateFormat   สับสนมาก หรือฉันกำลังพลาดอะไรอยู่?
                    DateFormatคุณสามารถใช้ ผลลัพธ์ขึ้นอยู่กับสถานที่เริ่มต้นของโทรศัพท์ แต่คุณสามารถระบุสถานที่ได้ด้วย:
https://developer.android.com/reference/java/text/DateFormat.html
นี่คือผลลัพธ์ใน
DateFormat.getDateInstance().format(date)                                          ตำแหน่งที่ตั้ง FR: 3 พ.ย. 2017
ตำแหน่งที่ตั้งของสหรัฐอเมริกา / En: 12 ม.ค. 1952
DateFormat.getDateInstance(DateFormat.SHORT).format(date)ตำแหน่งที่ตั้ง FR: 03/11/2017
สถานที่ US / En: 12.13.52
DateFormat.getDateInstance(DateFormat.MEDIUM).format(date)ตำแหน่งที่ตั้ง FR: 3 พ.ย. 2017
ตำแหน่งที่ตั้งของสหรัฐอเมริกา / En: 12 ม.ค. 1952
DateFormat.getDateInstance(DateFormat.LONG).format(date)สถานที่ FR: 3 พฤศจิกายน 2017
ตำแหน่งที่ตั้งของเรา / En: 12 มกราคม 2495
DateFormat.getDateInstance(DateFormat.FULL).format(date)สถานที่ FR: vendredi 3 พฤศจิกายน 2017
สถานที่สหรัฐฯ / En: วันอังคารที่ 12 เมษายน 2495
DateFormat.getDateTimeInstance().format(date)ตำแหน่งที่ตั้ง FR: 3 พ.ย. 2017 16:04:58 น
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date)สถานที่ FR: 03/11/2017 16:04
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(date)สถานที่ FR: 03/11/2017 16:04:58 น
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(date)ตำแหน่งที่ตั้ง FR: 03/11/2017 16:04:58 GMT + 01: 00
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL).format(date)สถานที่ FR: 03/11/2017 16:04:58 รักษาธรรมชาติ normale d'Europe centrale
DateFormat.getTimeInstance().format(date)ตำแหน่งที่ตั้ง FR: 16:04:58
DateFormat.getTimeInstance(DateFormat.SHORT).format(date)ตำแหน่งที่ตั้ง FR: 16:04
DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date)ตำแหน่งที่ตั้ง FR: 16:04:58
DateFormat.getTimeInstance(DateFormat.LONG).format(date)ตำแหน่งที่ตั้ง FR: 16:04:58 GMT + 01: 00
DateFormat.getTimeInstance(DateFormat.FULL).format(date)สถานที่ FR: 16:04:58 รักษา normale d'Europe centrale
สตริงวันที่ถึงสถานที่:
Date date = new Date();
String stringDate = DateFormat.getDateTimeInstance().format(date);ตัวเลือก:
   DateFormat.getDateInstance() -> 31 ธ.ค. 1969
   DateFormat.getDateTimeInstance() -> 31 ธันวาคม 1969 4:00:00 น
   DateFormat.getTimeInstance() -> 4:00:00 PM
สิ่งนี้จะทำ:
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));ใช้ SimpleDateFormat
แบบนี้:
event.putExtra("starttime", "12/18/2012");
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = format.parse(bundle.getString("starttime"));new SimpleDateFormat("my-format", Locale.getDefault());
                    ติดตามสิ่งนี้: http://developer.android.com/reference/android/text/format/Time.html
ดีกว่าที่จะใช้คลาส Android เนทิฟ:
Time now = new Time();
now.setToNow();จากนั้นจัดรูปแบบ:
Log.d("DEBUG", "Time "+now.format("%d.%m.%Y %H.%M.%S"));ใช้ทั้งสองนี้เป็นตัวแปรคลาส:
 public java.text.DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
 private Calendar mDate = null;และใช้มันเช่นนี้
 mDate = Calendar.getInstance();
 mDate.set(year,months,day);                   
 dateFormat.format(mDate.getTime());นี่คือวิธีการของฉันคุณสามารถกำหนดและรูปแบบอินพุตและเอาต์พุต
public static String formattedDateFromString(String inputFormat, String outputFormat, String inputDate){
    if(inputFormat.equals("")){ // if inputFormat = "", set a default input format.
        inputFormat = "yyyy-MM-dd hh:mm:ss";
    }
    if(outputFormat.equals("")){
        outputFormat = "EEEE d 'de' MMMM 'del' yyyy"; // if inputFormat = "", set a default output format.
    }
    Date parsed = null;
    String outputDate = "";
    SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
    SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());
    // You can set a different Locale, This example set a locale of Country Mexico.
    //SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("es", "MX"));
    //SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("es", "MX"));
    try {
        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);
    } catch (Exception e) { 
        Log.e("formattedDateFromString", "Exception in formateDateFromstring(): " + e.getMessage());
    }
    return outputDate;
}ฉันใช้ SimpleDateFormat โดยไม่มีรูปแบบที่กำหนดเองเพื่อรับวันที่และเวลาจริงจากระบบในรูปแบบที่เลือกไว้ล่วงหน้าของอุปกรณ์ :
public static String getFormattedDate() {
    //SimpleDateFormat called without pattern
    return new SimpleDateFormat().format(Calendar.getInstance().getTime());
}ผลตอบแทน :
ใช้บิลด์ในคลาส Time!
Time time = new Time();
time.set(0, 0, 17, 4, 5, 1999);
Log.i("DateTime", time.format("%d.%m.%Y %H:%M:%S"));คำตอบอื่น ๆ ที่ถูกต้องโดยทั่วไป ฉันต้องการที่จะสนับสนุนคำตอบที่ทันสมัย เรียนDate, DateFormatและSimpleDateFormatใช้ในส่วนของคำตอบอื่น ๆ จะล้าสมัยเป็นเวลานานและได้ก่อให้เกิดปัญหาสำหรับโปรแกรมเมอร์จำนวนมากเป็นเวลาหลายปี วันนี้เรามีสิ่งที่ดีกว่าอย่างมากในjava.timeAKA JSR-310 ซึ่งเป็นวันที่และเวลา Java API ที่ทันสมัย คุณสามารถใช้สิ่งนี้กับ Android ได้หรือไม่ มากที่สุดอย่างแน่นอน! คลาสที่ทันสมัยได้รับการ backported เพื่อ Android ในโครงการ ThreeTenABP ดูคำถามนี้: วิธีใช้ ThreeTenABP ใน Android Projectสำหรับรายละเอียดทั้งหมด
ตัวอย่างนี้คุณควรเริ่มต้น:
    int year = 2017, month = 9, day = 28, hour = 22, minute = 45;
    LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute);
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    System.out.println(dateTime.format(formatter));เมื่อฉันตั้งค่าภาษาที่ต้องการของคอมพิวเตอร์เป็น US English หรือ UK English สิ่งนี้จะพิมพ์:
Sep 28, 2017 10:45:00 PMเมื่อไหร่ที่ฉันตั้งค่าเป็นภาษาเดนมาร์กฉันจะได้รับ:
28-09-2017 22:45:00ดังนั้นมันจะปฏิบัติตามการกำหนดค่า ฉันไม่แน่ใจว่ารายละเอียดนั้นเป็นไปตามการตั้งค่าวันที่และเวลาของอุปกรณ์ของคุณอย่างไรและสิ่งนี้อาจแตกต่างกันไปตามโทรศัพท์
รหัสนี้ใช้ได้สำหรับฉัน!
Date d = new Date();
    CharSequence s = android.text.format.DateFormat.format("MM-dd-yy hh-mm-ss",d.getTime());
    Toast.makeText(this,s.toString(),Toast.LENGTH_SHORT).show();คลาสรูปแบบวันที่ทำงานร่วมกับรหัสโกงเพื่อให้วันที่ ชอบ
รหัสนี้จะส่งคืนวันที่และเวลาปัจจุบัน:
public String getCurrDate()
{
    String dt;
    Date cal = Calendar.getInstance().getTime();
    dt = cal.toLocaleString();
    return dt;
}เพื่อรับวันที่หรือเวลาในรูปแบบสถานที่เกิดเหตุจากมิลลิวินาทีฉันใช้สิ่งนี้:
Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault());
dateFormat.format(date);Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
dateFormat.format(date);Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
dateFormat.format(date);คุณสามารถใช้รูปแบบวันที่และรูปแบบเวลาอื่น ๆ ข้อมูลเพิ่มเติมเกี่ยวกับรูปแบบที่นี่
วิธีที่สั้นที่สุด:
// 2019-03-29 16:11
String.format("%1$tY-%<tm-%<td %<tR", Calendar.getInstance())%tRสั้นสำหรับ%tH:%tM, <วิธีการที่จะนำมาใช้พารามิเตอร์ที่ผ่านมา ( 1$)
มันเทียบเท่ากับ String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM", Calendar.getInstance())
https://developer.android.com/reference/java/util/Formatter.html
ลอง:
event.putExtra("startTime", "10/05/2012");และเมื่อคุณเข้าถึงตัวแปรที่ส่งผ่าน:
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(bundle.getString("startTime"));ฉันใช้มันแบบนี้:
public class DateUtils {
    static DateUtils instance;
    private final DateFormat dateFormat;
    private final DateFormat timeFormat;
    private DateUtils() {
        dateFormat = android.text.format.DateFormat.getDateFormat(MainApplication.context);
        timeFormat = android.text.format.DateFormat.getTimeFormat(MainApplication.context);
    }
    public static DateUtils getInstance() {
        if (instance == null) {
            instance = new DateUtils();
        }
        return instance;
    }
    public synchronized static String formatDateTime(long timestamp) {
        long milliseconds = timestamp * 1000;
        Date dateTime = new Date(milliseconds);
        String date = getInstance().dateFormat.format(dateTime);
        String time = getInstance().timeFormat.format(dateTime);
        return date + " " + time;
    }
}Java.util.Date และ .Calendar และ SimpleDateFormat ใน Java (และ Android) เป็นปัญหาที่ฉาวโฉ่ หลีกเลี่ยงพวกเขา พวกเขาแย่มากที่ Sun / Oracle ยอมแพ้พวกเขาแทนที่พวกเขาด้วยแพ็คเกจ java.time ใหม่ใน Java 8 (ไม่ใช่ใน Android ในปี 2014) ใหม่java.timeได้แรงบันดาลใจจากห้องสมุดJoda-Time
Joda-Timeทำงานบน Android
ค้นหา StackOverflow สำหรับ "Joda" เพื่อค้นหาตัวอย่างมากมายและการสนทนามากมาย
ชิ้นอาหารอันโอชะของซอร์สโค้ดโดยใช้ Joda-Time 2.4
รูปแบบมาตรฐาน
String output = DateTime.now().toString(); 
// Current date-time in user's default time zone with a String representation formatted to the ISO 8601 standard.รูปแบบที่มีการแปล
String output = DateTimeFormat.forStyle( "FF" ).print( DateTime.now() ); 
// Full (long) format localized for this user's language and culture.กลับไปที่ปี 2016 เมื่อฉันต้องการปรับแต่งรูปแบบ (ไม่เป็นไปตามการกำหนดค่าอุปกรณ์ตามที่คุณถาม ... ) ฉันมักจะใช้ไฟล์ทรัพยากรสตริง:
ใน strings.xml:
<string name="myDateFormat"><xliff:g id="myDateFormat">%1$td/%1$tm/%1$tY</xliff:g></string>ในกิจกรรม:
Log.d(TAG, "my custom date format: "+getString(R.string.myDateFormat, new Date()));นอกจากนี้ยังมีประโยชน์เมื่อมีการเปิดตัวDate Binding Libraryใหม่
ดังนั้นฉันสามารถมีอะไรเช่นนี้ในไฟล์เลย์เอาต์:
<TextView
    android:id="@+id/text_release_date"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:padding="2dp"
    android:text="@{@string/myDateFormat(vm.releaseDate)}"
    tools:text="0000"
    />และในคลาส Java:
    MovieDetailViewModel vm = new MovieDetailViewModel();
    vm.setReleaseDate(new Date());คลาสเวลาของ Android ให้วิธีการจัดรูปแบบ 3 วิธีhttp://developer.android.com/reference/android/text/format/Time.html
นี่คือวิธีที่ฉันทำ:
/**
* This method will format the data from the android Time class (eg. myTime.setToNow())   into the format
* Date: dd.mm.yy Time: hh.mm.ss
*/
private String formatTime(String time)
{
    String fullTime= "";
    String[] sa = new String[2];
    if(time.length()>1)
    {
        Time t = new Time(Time.getCurrentTimezone());
        t.parse(time);
        // or t.setToNow();
        String formattedTime = t.format("%d.%m.%Y %H.%M.%S");
        int x = 0;
        for(String s : formattedTime.split("\\s",2))
        {   
            System.out.println("Value = " + s);
            sa[x] = s;
            x++;
        }
        fullTime = "Date: " + sa[0] + " Time: " + sa[1];
    }
    else{
        fullTime = "No time data";
    }
    return fullTime;
}ฉันหวังว่าจะเป็นประโยชน์ :-)
มันสายเกินไป แต่มันอาจช่วยใครซักคน
DateFormat.format(format, timeInMillis);นี่formatคือรูปแบบที่คุณต้องการ
เช่น: "HH: mm" ส่งคืน 15:30