【问题标题】:Convert date to string in java? [closed]在java中将日期转换为字符串? [关闭]
【发布时间】:2013-06-26 07:01:36
【问题描述】:

在我的代码中,我得到的日期是这个字符串:

date="2013-06-15"

我希望它转换为 15 Jun 2013 。我试过 dateFormat:

DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
String date = df.format(det);   
DateFormat f2 = new SimpleDateFormat("dd-mmmm-yyyy");
date=f2.format(date).toLowerCase();
out.println("DATE"+date); 

但是它给出了空指针 任何人都可以帮我这样做吗?请帮忙?

【问题讨论】:

  • "字符串日期 = df.format(det);"什么是det?
  • NPE 扔到哪里去了?

标签: java date date-format simpledateformat


【解决方案1】:
String dateString="2013-06-15";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
DateFormat df2 = new SimpleDateFormat("dd MMM yyyy");
System.out.println(df.format(date).toLowerCase());//print 15-jun-2013
System.out.println(df.format(date));//print 15-Jun-2013
System.out.println(df2.format(date));//print 15 Jun 2013

【讨论】:

    【解决方案2】:

    首先将您的日期字符串转换为日期,然后将其转换为您所需的格式

    package naveed.workingfiles;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    
    public class DateToString {
    
    public static void main(String[] args) {
    
    String laDate="2013-06-15";
    String dateString = laDate.substring(8, 10) + "/"
                + laDate.substring(5, 7) + "/"
                + laDate.substring(0, 4);
    Date date= new SimpleDateFormat("dd/MM/yyyy").parse(dateString);
    String dateFormat = "dd-MMM-yyyy";
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, new Locale("en_US"));
    String tDate = sdf.format(date);
    System.out.println(tDate);//here your String date 
    
    }
    
    }
    

    【讨论】:

      【解决方案3】:
      // Create an instance of SimpleDateFormat used for formatting 
      // the string representation of date (month/day/year)
      DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
      
      // Get the date today using Calendar object.
      Date today = Calendar.getInstance().getTime();        
      // Using DateFormat format method we can create a string 
      // representation of a date with the defined format.
      String reportDate = df.format(today);
      
      // Print what date is today!
      System.out.println("Report Date: " + reportDate);
      

      【讨论】:

        【解决方案4】:

        这会起作用

            String dateStr = "2013-06-15";
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date date=df.parse(dateStr);
            DateFormat f2 = new SimpleDateFormat("dd-MMM-yyyy");
            System.out.println(f2.format(date));
        

        【讨论】:

          【解决方案5】:

          这行不通:format() 不接受字符串参数:

          String date = df.format(det);   
          DateFormat f2 = new SimpleDateFormat("dd-mmmm-yyyy");
          date=f2.format(date).toLowerCase();
          

          试试这个:

          String dateAsString = "2013-06-15"
          DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
          Date date = df.parse(dateAsString);   
          df = new SimpleDateFormat("dd MMM yyyy");
          out.println("DATE"+df.format(date)); 
          

          【讨论】:

          • 这其实是错的,M=月,m=分钟
          • @fareed 谢谢。该死的复制粘贴错误;)
          猜你喜欢
          • 2020-05-24
          • 2012-04-14
          • 2016-09-07
          • 1970-01-01
          • 1970-01-01
          • 2011-06-13
          • 1970-01-01
          相关资源
          最近更新 更多