chuyển đổi ngày giờ sang định dạng 24 giờ


83

Thời gian tôi nhận được từ máy chủ là như thế nào Jul 27, 2011 8:35:29 AM.

Tôi muốn chuyển đổi nó thành yyyy-MM-dd HH:mm:ss.

Tôi cũng muốn thời gian được chuyển đổi ở định dạng 24 giờ. Bất kỳ ai có thể đưa ra giải pháp cho vấn đề này. Đầu ra tôi muốn nhận được giống như2011-07-27 08:35:29

Câu trả lời:


120

Thử đi:

String dateStr = "Jul 27, 2011 8:35:29 AM";
DateFormat readFormat = new SimpleDateFormat( "MMM dd, yyyy hh:mm:ss aa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
    date = readFormat.parse(dateStr);
} catch (ParseException e) {
    e.printStackTrace();
}

if (date != null) {
    String formattedDate = writeFormat.format(date);
}

5
Tôi không thể chuyển đổi từ 2:46 chiều thành 14:46 bằng mã này. Tui bỏ lỡ điều gì vậy?
pnv

@ user1930402, bạn có thể thực hiện việc này bằng cách thay đổi định dạng đọc thành hh:mm a- đầu vào của bạn bỏ qua giây (chỉ là phỏng đoán).
Dhruv Ghulati

1
khi chúng ta có aa ở định dạng chắc chắn rằng hh là trường hợp nhỏ khác, nó phải ở trong capitol
Narsosystemdy

1
Rất nhỏ và khó xác định nếu bạn không biết về mẫu định dạng ngày. Đối với tôi, nó chỉ là một sự thay đổi hhđến HHvà vấn đề được giải quyết!
S_K

188

H vs h là sự khác biệt giữa định dạng 24 giờ và 12 giờ.


1
Vị cứu tinh ban ngày. Cảm ơn.
Farooq Arshed

2
Cảm ơn bạn vì câu trả lời rất đơn giản. Không cần đoạn mã
aianitro

1
Dành cho người mới: DateFormat format = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
AlexS,

Thiên tài. Cảm ơn!
Giedrius Šlikas

97
kk = Hours in 1-24 format
hh= hours in 1-12 format
KK= hours in 0-11 format
HH= hours in 0-23 format

11

Thử đi:

Date date=new Date("12/12/11 8:22:09 PM");    
System.out.println("Time in 24Hours ="+new SimpleDateFormat("HH:mm").format(date));


4

Tôi đang lấy một ví dụ về ngày tháng đưa ra dưới đây và in hai định dạng ngày tháng khác nhau theo yêu cầu của bạn.

String date="01/10/2014 05:54:00 PM";

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa",Locale.getDefault());

try {
            Log.i("",""+new SimpleDateFormat("ddMMyyyyHHmmss",Locale.getDefault()).format(simpleDateFormat.parse(date)));

            Log.i("",""+new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",Locale.getDefault()).format(simpleDateFormat.parse(date)));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Nếu bạn vẫn có bất kỳ câu hỏi nào, Vui lòng trả lời. Cảm ơn.


2
import java.text.SimpleDateFormat;

import java.util.Date;

public class DateFormatExample {

public static void main(String args[]) {

    // This is how to get today's date in Java
    Date today = new Date();

    //If you print Date, you will get un formatted output
    System.out.println("Today is : " + today);

    //formatting date in Java using SimpleDateFormat
    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
    String date = DATE_FORMAT.format(today);
    System.out.println("Today in dd-MM-yyyy format : " + date);

    //Another Example of formatting Date in Java using SimpleDateFormat
    DATE_FORMAT = new SimpleDateFormat("dd/MM/yy");
    date = DATE_FORMAT.format(today);
    System.out.println("Today in dd/MM/yy pattern : " + date);

    //formatting Date with time information
    DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS");
    date = DATE_FORMAT.format(today);
    System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);

    //SimpleDateFormat example - Date with timezone information
    DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS Z");
    date = DATE_FORMAT.format(today);
    System.out.println("Today in dd-MM-yy:HH:mm:SSZ : " + date);

} 

}

Đầu ra:

Hôm nay là: Thứ Sáu, 02 Tháng 11 16:11:27 IST 2012

Hôm nay ở định dạng dd-MM-yyyy: 02-11-2012

Hôm nay theo mô hình dd / MM / yy: 02/11/12

Hôm nay trong dd-MM-yy: HH: mm: SS: 02-11-12: 16: 11: 316

Hôm nay trong dd-MM-yy: HH: mm: SSZ: 02-11-12: 16: 11: 316 +0530


2

TRONG hai dòng:

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm aa",Locale.getDefault());
        FAJR = new SimpleDateFormat("HH:mm",Locale.getDefault()).format(simpleDateFormat.parse("8:35 PM");

2
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Date date = new Date();
    Date date2 = new Date("2014/08/06 15:59:48");

    String currentDate = dateFormat.format(date).toString();
    String anyDate = dateFormat.format(date2).toString();

    System.out.println(currentDate);
    System.out.println(anyDate);

1

chỉ là một mẹo,

cố gắng sử dụng JodaTime thay vì java, use.Date nó mạnh mẽ hơn nhiều và nó có một phương thức toString ("") để bạn có thể chuyển định dạng bạn muốn như toString ("yyy-MM-dd HH: mm: ss") ;

http://joda-time.sourceforge.net/


1

Với Lịch, nó hoạt động như sau:

    //create first Calendar object
    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.HOUR_OF_DAY, 15); // 3 PM
    // the same is
    calendar.set(Calendar.AM_PM, Calendar.PM); // choose PM mode
    calendar.set(Calendar.HOUR, 3); // 3 PM

    System.out.println( calendar.get(Calendar.HOUR) ); // AM_PM format
    System.out.println( calendar.get(Calendar.HOUR_OF_DAY) ); // 0-23 format

0

Đây là một phương pháp sử dụng JODA.

Ví dụ đầu vào là 07/20/2015 01:46:34.436 AM

public static String get24HourFormat(String dateString){
        Date date = new Date();
        DateTime date1=new DateTime();

        //  String date="";
        int hour=0;
        //int year=0;
        if(dateString!=null){

            try {
                DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm:ss.SSS aa");
                DateTimeFormatter output = DateTimeFormat.forPattern("kk");

                date1=formatter.parseDateTime(dateString);
                hour=date1.getHourOfDay();
                System.out.println(output.print(date1));
                //  System.out.println(date);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return hour+"";
    }
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.