البرمجة

تحويل تاريخ رقمي إلى تاريخ نصي في جافا

To convert the numeric date 201509122150 to the Date class in the YYYY-MM-DD hh:mm format in Java, you can follow these steps:

  1. Parsing the Numeric Date: First, you need to parse the numeric date into a format that can be understood by the Date class. For this, you can use SimpleDateFormat.

  2. Formatting the Date: Once you have parsed the numeric date into a Date object, you can then format it into the desired format using another SimpleDateFormat.

Here’s a code example to demonstrate this:

java
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateConversion { public static void main(String[] args) { String numericDate = "201509122150"; // Define the format of the numeric date SimpleDateFormat numericFormat = new SimpleDateFormat("yyyyMMddHHmm"); try { // Parse the numeric date into a Date object Date date = numericFormat.parse(numericDate); // Define the desired output format SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // Format the date into the desired format String formattedDate = outputFormat.format(date); System.out.println("Formatted Date: " + formattedDate); } catch (ParseException e) { System.out.println("Error parsing date: " + e.getMessage()); } } }

This code will output:

yaml
Formatted Date: 2015-09-12 21:50

In this code, we first parse the numeric date using the format yyyyMMddHHmm to convert it into a Date object. Then, we define the output format as yyyy-MM-dd HH:mm and use it to format the Date object into a string representing the desired format.

المزيد من المعلومات

لتحويل التاريخ الرقمي 201509122150 إلى كائن Date في تنسيق YYYY-MM-DD hh:mm في جافا، يمكنك اتباع الخطوات التالية:

  1. تحويل التاريخ الرقمي: أولاً، يجب تحويل التاريخ الرقمي إلى تنسيق يمكن فهمه من قبل كائن Date، يمكنك استخدام SimpleDateFormat لهذا الغرض.

  2. تنسيق التاريخ: بمجرد تحويل التاريخ الرقمي إلى كائن Date، يمكنك بعد ذلك تنسيقه إلى التنسيق المطلوب باستخدام SimpleDateFormat أيضًا.

اليك كود مثال يوضح ذلك:

java
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateConversion { public static void main(String[] args) { String numericDate = "201509122150"; // تحديد تنسيق التاريخ الرقمي SimpleDateFormat numericFormat = new SimpleDateFormat("yyyyMMddHHmm"); try { // تحويل التاريخ الرقمي إلى كائن Date Date date = numericFormat.parse(numericDate); // تحديد التنسيق المطلوب للمخرجات SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // تنسيق التاريخ إلى التنسيق المطلوب String formattedDate = outputFormat.format(date); System.out.println("Formatted Date: " + formattedDate); } catch (ParseException e) { System.out.println("Error parsing date: " + e.getMessage()); } } }

هذا الكود سيقوم بطباعة:

yaml
Formatted Date: 2015-09-12 21:50

في هذا الكود، نقوم أولاً بتحويل التاريخ الرقمي باستخدام التنسيق yyyyMMddHHmm لتحويله إلى كائن Date. ثم، نحدد التنسيق المطلوب كـ yyyy-MM-dd HH:mm ونستخدمه لتنسيق كائن Date إلى سلسلة نصية تمثل التنسيق المطلوب.

مقالات ذات صلة

زر الذهاب إلى الأعلى

المحتوى محمي من النسخ !!