在Java中转换MySQL日期格式 [英] Converting MySQL date format in Java

查看:113
本文介绍了在Java中转换MySQL日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次在这里接触Java,所以对我轻松一点.我必须更正别人的旧代码.这个Java程序从MySQL表中获取数据,并准备将其显示在网页上.我发现他们的MySQL表出于某种原因在所有日期中都使用了INT字段.因此,例如,今天的日期存储为INT3222017.我首先必须向MySQL表中添加适当的DATE列.接下来,我重新创建另一个Java脚本,以从该脚本将使用的MySQL表的更大表中提取数据.

I am touching Java for the very first time here, so go easy on me. I am having to correct someone else's older code. This java program takes data from a MySQL table and prepares it for presentation on web pages. I found that their MySQL table for some reason used INT fields for all the dates. So, for instance, today's date is stored as the INT 3222017. I first had to add the proper DATE columns to the MySQL table. Next I re-did another Java script to pull the data from a larger table for the MySQL table that this script will be using.

现在,我需要更改当前将INT字段格式化为MM/dd/yyyy的代码,以采用DATE字段(存储为yyyy-MM-dd)并将它们转换为相同的MM/dd/yyyy.

Now I need to change the code that currently formats the INT fields into MM/dd/yyyy to instead take the DATE fields (stored as yyyy-MM-dd) and convert them to the same MM/dd/yyyy.

以下是在INT字段上使用dateFormat函数的一部分代码-

Here is a piece of the code that uses the function dateFormat on the INT fields -

String formattedDate = "";

            formattedDate = dateFormat(oneSpectro.getSpectroLastDate());
            result.setSpectroLastDate(formattedDate);

            formattedDate = dateFormat(oneSpectro.getSpectroOrigDate());
            result.setSpectroOrigDate(formattedDate);

            formattedDate = dateFormat(oneSpectro.getSpectroCalDate());
            result.setSpectroCalDate(formattedDate);
            }

        return result;
    }

这是dateFormat函数-

And here is the dateFormat function -

public String dateFormat(int theDate){
        String      dateString = "";
        Integer     dateInt = 0;

        if (theDate < 10000000){
            dateInt = new Integer(theDate);
            dateString = dateInt.toString();
            // Add preceding 0 to make the date 8 digits in length.
            dateString  = "0" + dateString;

        }
        else {
            // Process an 8 digit date.
            dateInt = new Integer(theDate);
            dateString = dateInt.toString();
        }
        // Install date slashes in the date string.
        if (dateString.length() == 8){
            dateString = dateString.substring(0, 2) + "/" + dateString.substring(2, 4) + "/" 
                    + dateString.substring(4);
        }
        return dateString;
    }

如何将DATE字段(yyyy-MM-dd)转换为格式为MM/dd/yyyy的日期?

How do I go about converting a DATE field (yyyy-MM-dd) to a formatted date of MM/dd/yyyy?

推荐答案

您可以将String转换为Date util:

You can convert String to Date util:

 DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
 Date date = format.parse(your string);

然后将Date对象转换为您的String格式:

And then convert the Date object to your String format:

 DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
 String result = df.format(date);

这篇关于在Java中转换MySQL日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆