Convert java.util.Date to String

前端 未结 18 2303
悲&欢浪女
悲&欢浪女 2020-11-22 05:37

I want to convert a java.util.Date object to a String in Java.

The format is 2010-05-30 22:15:52

相关标签:
18条回答
  • 2020-11-22 05:46

    Altenative one-liners in plain-old java:

    String.format("The date: %tY-%tm-%td", date, date, date);
    
    String.format("The date: %1$tY-%1$tm-%1$td", date);
    
    String.format("Time with tz: %tY-%<tm-%<td %<tH:%<tM:%<tS.%<tL%<tz", date);
    
    String.format("The date and time in ISO format: %tF %<tT", date);
    

    This uses Formatter and relative indexing instead of SimpleDateFormat which is not thread-safe, btw.

    Slightly more repetitive but needs just one statement. This may be handy in some cases.

    0 讨论(0)
  • 2020-11-22 05:49
    public static String formateDate(String dateString) {
        Date date;
        String formattedDate = "";
        try {
            date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.getDefault()).parse(dateString);
            formattedDate = new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault()).format(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return formattedDate;
    }
    
    0 讨论(0)
  • 2020-11-22 05:50

    The easiest way to use it is as following:

    currentISODate = new Date().parse("yyyy-MM-dd'T'HH:mm:ss", "2013-04-14T16:11:48.000");
    

    where "yyyy-MM-dd'T'HH:mm:ss" is the format of the reading date

    output: Sun Apr 14 16:11:48 EEST 2013

    Notes: HH vs hh - HH refers to 24h time format - hh refers to 12h time format

    0 讨论(0)
  • 2020-11-22 05:52

    Here are examples of using new Java 8 Time API to format legacy java.util.Date:

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS Z")
            .withZone(ZoneOffset.UTC);
        String utcFormatted = formatter.format(date.toInstant()); 
    
        ZonedDateTime utcDatetime = date.toInstant().atZone(ZoneOffset.UTC);
        String utcFormatted2 = utcDatetime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS Z"));
        // gives the same as above
    
        ZonedDateTime localDatetime = date.toInstant().atZone(ZoneId.systemDefault());
        String localFormatted = localDatetime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
        // 2011-12-03T10:15:30+01:00[Europe/Paris]
    
        String nowFormatted = LocalDateTime.now().toString(); // 2007-12-03T10:15:30.123
    

    It is nice about DateTimeFormatter that it can be efficiently cached as it is thread-safe (unlike SimpleDateFormat).

    List of predefined fomatters and pattern notation reference.

    Credits:

    How to parse/format dates with LocalDateTime? (Java 8)

    Java8 java.util.Date conversion to java.time.ZonedDateTime

    Format Instant to String

    What's the difference between java 8 ZonedDateTime and OffsetDateTime?

    0 讨论(0)
  • 2020-11-22 05:52

    Let's try this

    public static void main(String args[]) {
    
        Calendar cal = GregorianCalendar.getInstance();
        Date today = cal.getTime();
        DateFormat df7 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        try {           
            String str7 = df7.format(today);
            System.out.println("String in yyyy-MM-dd format is: " + str7);          
        } catch (Exception ex) {
          ex.printStackTrace();
        }
    }
    

    Or a utility function

    public String convertDateToString(Date date, String format) {
        String dateStr = null;
        DateFormat df = new SimpleDateFormat(format);
    
        try {
            dateStr = df.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dateStr;
    }
    

    From Convert Date to String in Java

    0 讨论(0)
  • 2020-11-22 05:56

    Convert a Date to a String using DateFormat#format method:

    String pattern = "MM/dd/yyyy HH:mm:ss";
    
    // Create an instance of SimpleDateFormat used for formatting 
    // the string representation of date according to the chosen pattern
    DateFormat df = new SimpleDateFormat(pattern);
    
    // Get the today date 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 todayAsString = df.format(today);
    
    // Print the result!
    System.out.println("Today is: " + todayAsString);
    

    From http://www.kodejava.org/examples/86.html

    0 讨论(0)
提交回复
热议问题