Convert qua lại giữa Date và String trong Java

Bài viết mục đích để sau này tra lại cho nhanh :)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public static String convertDateToString(Date date, String format) {
    SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
    return mySimpleDateFormat.format(date);
}

public static Date convertStringToDate(String dateStr, String format) {
    SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
    try {
        return mySimpleDateFormat.parse(dateStr);
    } catch (ParseException e) {
        return null;
    }
}
Advertisements

One thought on “Convert qua lại giữa Date và String trong Java

  1. I’ve been surfing on-line greater than 3 hours these days, yet I never discovered any fascinating article like yours.
    It’s beautiful value enough for me. In my opinion, if all webmasters and bloggers made just
    right content material as you probably did, the web will probably be a lot more useful than ever before.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s