在android中将字符串转换为日期格式

时间:2012-01-16 10:47:13

标签: java android datetime datetime-format

我正在尝试将字符串转换为日期格式。我尝试了很多方法来做到这一点。但是没有成功。我的字符串是“2012年1月17日”。我想将其转换为“2011-10-17”。 有人可以告诉我这样做的方法吗?如果您有任何实例,那将是一个真正的帮助!

7 个答案:

答案 0 :(得分:30)

public class Utils {

    public static void main(String[] args) {

        String mytime="Jan 17, 2012";
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "MMM dd, yyyy");

        Date myDate = null;
        try {
            myDate = dateFormat.parse(mytime);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
        String finalDate = timeFormat.format(myDate);

        System.out.println(finalDate);
    }
}

答案 1 :(得分:5)

   String format = "yyyy-MM-dd";
   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));

在PDT时区中运行时产生此输出:

                 yyyy-MM-dd 1969-12-31
                 yyyy-MM-dd 1970-01-01

有关详细信息,请查看here

答案 2 :(得分:2)

我建议使用Joda Time,它是Java中日期/日期时间操作的最佳和最简单的库,它是ThreadSafe(与Java中的默认格式化类相对)。 / p>

你这样使用它:

// Define formatters:
DateTimeFormatter inputFormat = DateTimeFormat.forPattern("MMM dd, yyyy");
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy-MM-dd");

// Do your conversion:
String inputDate = "Jan 17, 2012";
DateTime date = inputFormat.parseDateTime(inputDate);
String outputDate = outputFormat.print(date);
// or:
String outputDate = date.toString(outputFormat);
// or:
String outputDate = date.toString("yyyy-MM-dd");

// Result: 2012-01-17

它还为日期操作提供了大量有用的方法(添加日期,时差等)。它为大多数类提供了接口,以便于测试和依赖注入。

答案 3 :(得分:1)

为什么要将字符串转换为字符串尝试将当前时间(以毫秒为单位)转换为格式化字符串, 此方法会将您的milisconds转换为数据格式。

 public static String getTime(long milliseconds)
{

         return DateFormat.format("MMM dd, yyyy", milliseconds).toString();
}

您还可以尝试DATE FORMATE课程以便更好地理解。

答案 4 :(得分:1)

You can't convert date from one format to other. while you are taking the date take you have take the date which ever format the you want. If you want the date in yyyy-mm-dd. You can get this by using following way.

            java.util.Calendar calc = java.util.Calendar.getInstance();

        int day = calc.get(java.util.Calendar.DATE);
        int month = calc.get(java.util.Calendar.MONTH)+1;
        int year = calc.get(java.util.Calendar.YEAR);

           String currentdate = year +"/"+month +"/"+day ;

答案 5 :(得分:0)

public static Date getDateFromString(String date) {

    Date dt = null;

    if (date != null) {
        for (String sdf : supportedDateFormats) {
            try {
                dt = new Date(new SimpleDateFormat(sdf).parse(date).getTime());
                break;
            } catch (ParseException pe) {
                pe.printStackTrace();
            }
        }
    }
    return dt;
}

答案 6 :(得分:0)

试试这个简单的方法:

        fun getFormattedDate(strDate:String): String {
        try {
            val dateFormat = SimpleDateFormat("dd/mm/yyyy")//old format
            val dateFormat2 = SimpleDateFormat("mm/dd/yyyy")//require new formate
            val objDate = dateFormat.parse(strDate)
            return dateFormat2.format(objDate)
        } catch (e:Exception) {
            return ""
        }
    }
相关问题