我如何将 java.sql.Date 格式化为 MM/dd/yyyy
how can i format the java.sql.Date to MM/dd/yyyy
我的密码是
final long MILLIS_IN_A_DAY = 1000*60*60*24;
Date yesterdaydate= new java.sql.Date(new java.util.Date().getTime() - MILLIS_IN_A_DAY);
out.println(yesterdaydate);
我得到的输出格式为 yyyy/mm/dd..我怎样才能在 MM/dd/yyyy
中得到它
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String DateToStr = format.format(yesterdaydate);
out.println(DateToStr);
请不要那样做。使用 Date
对象获取日期并使用 SimpleDateFormat
解析它。这很简单 。不要硬编码值。这对程序员来说不方便。请找到代码
Date date = new Date();//Today Date
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");// FOrmat in This Format or you change Change as well
String Format= format.format(date);
System.out.println(Format);// 01/06/2016
或
Calendar TodayDate = Calendar.getInstance();
TodayDate.add(Calendar.DAY_ON_MONTH, -1);//import java.util.*
Date yesterday = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String Format = format.format(yesterday);
System.out.println(Format);// 01/06/2016
要获取昨天的日期并对其进行格式化,您有 3 个选择:
// Use Java 8+ LocalDate
java.time.LocalDate yesterday = LocalDate.now().minusDays(1);
String text = yesterday.format(DateTimeFormatter.ofPattern("MM/dd/uuuu"));
// Use Joda-Time (3rd-party library)
org.joda.time.LocalDate yesterday = LocalDate.now().minusDays(1);
String text = yesterday.toString("MM/dd/yyyy");
// Use Calendar (all Java versions, no 3rd-party library)
java.util.Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_ON_MONTH, -1);
java.util.Date yesterday = cal.getTime();
String text = new SimpleDateFormat("MM/dd/yyyy").format(yesterday);
请注意,Calendar
版本仍然使用 time-of-day 生成 "date" 值。需要额外的代码来清除时间字段。
我的密码是
final long MILLIS_IN_A_DAY = 1000*60*60*24;
Date yesterdaydate= new java.sql.Date(new java.util.Date().getTime() - MILLIS_IN_A_DAY);
out.println(yesterdaydate);
我得到的输出格式为 yyyy/mm/dd..我怎样才能在 MM/dd/yyyy
中得到它SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String DateToStr = format.format(yesterdaydate);
out.println(DateToStr);
请不要那样做。使用 Date
对象获取日期并使用 SimpleDateFormat
解析它。这很简单 。不要硬编码值。这对程序员来说不方便。请找到代码
Date date = new Date();//Today Date
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");// FOrmat in This Format or you change Change as well
String Format= format.format(date);
System.out.println(Format);// 01/06/2016
或
Calendar TodayDate = Calendar.getInstance();
TodayDate.add(Calendar.DAY_ON_MONTH, -1);//import java.util.*
Date yesterday = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String Format = format.format(yesterday);
System.out.println(Format);// 01/06/2016
要获取昨天的日期并对其进行格式化,您有 3 个选择:
// Use Java 8+ LocalDate
java.time.LocalDate yesterday = LocalDate.now().minusDays(1);
String text = yesterday.format(DateTimeFormatter.ofPattern("MM/dd/uuuu"));
// Use Joda-Time (3rd-party library)
org.joda.time.LocalDate yesterday = LocalDate.now().minusDays(1);
String text = yesterday.toString("MM/dd/yyyy");
// Use Calendar (all Java versions, no 3rd-party library)
java.util.Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_ON_MONTH, -1);
java.util.Date yesterday = cal.getTime();
String text = new SimpleDateFormat("MM/dd/yyyy").format(yesterday);
请注意,Calendar
版本仍然使用 time-of-day 生成 "date" 值。需要额外的代码来清除时间字段。