在java中从MM/DD/YYYY到DD-MMM-YYYY

发布于 2024-10-02 08:37:28 字数 136 浏览 6 评论 0原文

Java 中是否有一种方法可以将 MM/DD/YYYY 转换为 DD-MMM-YYYY

例如:05/01/199901-MAY-99

Is there a method in Java that I can use to convert MM/DD/YYYY to DD-MMM-YYYY?

For example: 05/01/1999 to 01-MAY-99

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

谁对谁错谁最难过 2024-10-09 08:37:28

使用 SimpleDateFormat 解析日期,然后使用具有所需格式的 SimpleDateFormat 打印出来。

这是一些代码:

    SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
    SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yy");
    Date date = format1.parse("05/01/1999");
    System.out.println(format2.format(date));

输出:

01-May-99

Use a SimpleDateFormat to parse the date and then print it out with a SimpleDateFormat withe the desired format.

Here's some code:

    SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
    SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yy");
    Date date = format1.parse("05/01/1999");
    System.out.println(format2.format(date));

Output:

01-May-99
极致的悲 2024-10-09 08:37:28

java.time

您应该在 Java 8 及更高版本中使用 java.time 类。要使用java.time,请添加:

import java.time.* ;

下面是一个如何格式化日期的示例。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
String date = "15-Oct-2018";
LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate); 
System.out.println(formatter.format(localDate));

java.time

You should use java.time classes with Java 8 and later. To use java.time, add:

import java.time.* ;

Below is an example, how you can format date.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
String date = "15-Oct-2018";
LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate); 
System.out.println(formatter.format(localDate));
冷…雨湿花 2024-10-09 08:37:28

试试这个,

Date currDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String strCurrDate = dateFormat.format(currDate);
System.out.println("strCurrDate->"+strCurrDate);

Try this,

Date currDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String strCurrDate = dateFormat.format(currDate);
System.out.println("strCurrDate->"+strCurrDate);
渡你暖光 2024-10-09 08:37:28
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.now();
System.out.println("Formatted Date: " + formatter.format(localDate));

Java 8 本地日期

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.now();
System.out.println("Formatted Date: " + formatter.format(localDate));

Java 8 LocalDate

自我难过 2024-10-09 08:37:28

试试这个

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

Try this

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // Set your date format
String currentData = sdf.format(new Date());
玉环 2024-10-09 08:37:28

java.time

java.util Date-Time API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用它们并切换到 现代日期时间 API*

日期时间解析/格式化类型是区域设置敏感的

日期时间解析/格式化类型(例如DateTimeFormatter)是区域设置敏感的,即相同的字母将在不同的 Locale 中生成文本。例如 MMM 用于月份名称的三个字母缩写,并且在不同的 中可以是不同的单词区域设置。 如果没有Locale参数,它将使用JVM的Locale。因此,永远不要忘记使用不带 Locale 参数的日期时间解析/格式化类型。通过切勿在没有区域设置的情况下使用 SimpleDateFormat 或 DateTimeFormatter 了解更多信息。

您需要 DateTimeFormatter 的两个实例 - 一个用于解析输入字符串,另一个用于根据所需模式格式化输出字符串。

演示:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
        String strDate = "05/01/1999";
        LocalDate date = LocalDate.parse(strDate, dtfInput);

        // The default string i.e. the value returned by LocalDate#toString
        System.out.println(date);

        DateTimeFormatter dtfOutputEng = DateTimeFormatter.ofPattern("dd-MMM-uuuu", Locale.ENGLISH);
        String formattedEng = dtfOutputEng.format(date);
        System.out.println(formattedEng);

        DateTimeFormatter dtfOutputFr = DateTimeFormatter.ofPattern("dd-MMM-uuuu", Locale.FRENCH);
        String formattedFr = dtfOutputFr.format(date);
        System.out.println(formattedFr);
    }
}

输出:

1999-05-01
01-May-1999
01-mai-1999

在线演示

其他一些重要说明:

  1. 而不是 Yweek-based-year),您需要使用yyear-of-era)而不是D年份),您需要使用d月份)。查看文档了解更多信息关于它。
  2. 在这里,您可以使用 y 而不是 u,但我更喜欢 uy

了解有关现代日期时间 API* 来自 跟踪:日期时间< /a>


* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API如何在Android项目中使用ThreeTenABP

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

A Date-Time parsing/formatting type is Locale-sensitive

A Date-Time parsing/formatting type (e.g. DateTimeFormatter) is Locale-sensitive i.e. the same letters will produce the text in different Locales .e.g. MMM is used for the three-letter abbreviation of month name and it can be different words in different Locales. In the absence of the Locale parameter, it will use the JVM's Locale. Therefore, never forget to use a Date-Time parsing/formatting type without the Locale parameter. Learn more about it from Never use SimpleDateFormat or DateTimeFormatter without a Locale.

You need two instances of DateTimeFormatter - one to parse the input string and another to format the output string, as per required patterns.

Demo:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
        String strDate = "05/01/1999";
        LocalDate date = LocalDate.parse(strDate, dtfInput);

        // The default string i.e. the value returned by LocalDate#toString
        System.out.println(date);

        DateTimeFormatter dtfOutputEng = DateTimeFormatter.ofPattern("dd-MMM-uuuu", Locale.ENGLISH);
        String formattedEng = dtfOutputEng.format(date);
        System.out.println(formattedEng);

        DateTimeFormatter dtfOutputFr = DateTimeFormatter.ofPattern("dd-MMM-uuuu", Locale.FRENCH);
        String formattedFr = dtfOutputFr.format(date);
        System.out.println(formattedFr);
    }
}

Output:

1999-05-01
01-May-1999
01-mai-1999

ONLINE DEMO

Some other important notes:

  1. Instead of Y (week-based-year), you need to use y (year-of-era) and instead of D (day-of-year), you need to use d (day-of-month). Check the documentation to learn more about it.
  2. Here, you can use y instead of u but I prefer u to y.

Learn more about the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

属性 2024-10-09 08:37:28

下面应该可以工作。

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
Date oldDate = df.parse(df.format(date)); //this date is your old date object

Below should work.

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
Date oldDate = df.parse(df.format(date)); //this date is your old date object
手心的温暖 2024-10-09 08:37:28
formatter = new SimpleDateFormat("dd-MMM-yy");
formatter = new SimpleDateFormat("dd-MMM-yy");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文