我想将日期3 7 2015更改为3Aug 2015
我有日期值,如:
int day=3,month =7 ,year =2015
答案 0 :(得分:1)
这是利用新的date and formatter classes in Java 8:
LocalDateTime date = LocalDate.of(2015, Month.JULY, 3);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dMMM yyyy");
String formattedDateTime = date.format(formatter); // "3Aug 2015"
答案 1 :(得分:1)
您需要使用真实的Date对象,然后对其进行格式化:
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 3);
cal.set(Calendar.MONTH, 6);
cal.set(Calendar.YEAR, 2015);
SimpleDateFormat format = new SimpleDateFormat("DD_MMM_YYYY");
format.format(cal.getTime());
答案 2 :(得分:0)
在示例代码下面进行简单的尝试: -
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateFormatter {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(2015, 7, 3);
SimpleDateFormat sdf = new SimpleDateFormat("dMMM yyyy");
System.out.println("Formatted Date : " + sdf.format(cal.getTime()));
}
}
<强>输出强>
Formatted Date : 3Aug 2015
答案 3 :(得分:0)
// Convert all the value to string, then concate the value in the following format
// date Month year, then use Simpledateformat as dd MMM yyyy
DateFormat originalFormat = new SimpleDateFormat("dd MM yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy");
Date date = originalFormat.parse("3 7 2015");
String formattedDate = targetFormat.format(date);
答案 4 :(得分:-1)
如果您在变量中有日期值,那么您可以使用数组来获取月份的值。
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int day = 5;
int month = 4;
int year = 1994;
System.out.printnln("The Date is " + day + months[month - 1]+ year);