似乎PHP使用其他日期格式符号而不是Java来根据模式格式化日期。 例如。当我想输出这样的日期时:
17.03.2014 09:50
在PHP中,我必须使用日期格式模式
d.m.Y H:i
在Java中,我使用模式
dd.mm.yyyy hh:mm
是否有Java类,可以将给定的输入(Java)模式转换为PHP模式,例如将dd.mm.yyyy hh:mm
转换为d.m.Y H:i
?我问,因为我必须使用Javascript date picker,它使用PHP日期格式符号来格式化日期。
答案 0 :(得分:0)
抱歉,没有Java类可以执行此转换。你必须根据自己的需要从零开始编写自己的。
注意:每种编程语言都使用自己的模式方言。甚至Java自版本8以来都有两种不同的模式样式,但它们之间没有转换!
补充编辑:
您还应该记住,由于底层不同格式和解析引擎的限制,转换并不总是可行的。此外,有时可以证明新模式方言的中断和引入是合理的,请参阅JSR-310的设计决策,以获得更加类似CLDR和更强大的Java模式方言。
答案 1 :(得分:0)
这是我写的,它依赖于JodaTime库。我没有对每个案例进行深度测试,所以请确保它对你有用。
import org.joda.time.DateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class DateUtils {
private static final String[] daysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
private static final String[] daysOfWeekShort = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"};
private static final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
private static final String[] monthsShort = {"Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
private static final String[] daysInMonth = {"31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"};
private static final String[] suffixes = {"st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th",
"th", "th", "th", "th", "th", "th", "th", "th", "th", "th",
"st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th",
"th", "th"};
/*
Works like PHP Date function, has support for most characters. Time zone offsets mainly what is missing.
*/
public static String format(DateTime dt, String format){
int pos = 0;
char[] charArray = format.toCharArray();
int length = charArray.length;
StringBuilder sb = new StringBuilder();
while (pos < length){
switch(charArray[pos]){
// Days
case 'd':
sb.append(dt.getDayOfMonth() < 10 ? "0" + dt.getDayOfMonth() : dt.getDayOfMonth());
break;
case 'D':
sb.append(daysOfWeekShort[dt.getDayOfWeek()]);
break;
case 'l':
sb.append(daysOfWeek[dt.getDayOfWeek()]);
break;
case 'j':
sb.append(dt.getDayOfMonth());
break;
case 'N':
sb.append(dt.getDayOfWeek() + 1);
break;
case 'S':
sb.append(suffixes[dt.getDayOfMonth() - 1]);
case 'w':
sb.append(dt.getDayOfWeek());
break;
case 'z':
sb.append(dt.getDayOfYear());
break;
// Weeks
case 'W':
sb.append(dt.getWeekOfWeekyear()); // Needs confirmation
break;
// Months
case 'F':
sb.append(months[dt.getMonthOfYear() - 1]);
break;
case 'M':
sb.append(monthsShort[dt.getMonthOfYear() - 1]);
break;
case 'm':
sb.append(dt.getMonthOfYear() < 10 ? "0" + dt.getMonthOfYear() : dt.getMonthOfYear());
break;
case 'n':
sb.append(dt.getMonthOfYear());
break;
case 't':
int moy = dt.getMonthOfYear();
if (moy == 2 && dt.getYear() % 4 == 0) { sb.append("29"); }
else { sb.append(daysInMonth[moy]); }
break;
// Years
case 'Y':
sb.append(dt.getYear());
break;
case 'y':
sb.append(dt.getYearOfCentury());
break;
case 'L':
sb.append(dt.getYearOfCentury() % 4 == 0 ? 1 : 0);
break;
// AM/PM
case 'a':
sb.append(dt.getHourOfDay() < 13 ? "am" : "pm");
break;
case 'A':
sb.append(dt.getHourOfDay() < 13 ? "AM" : "PM");
break;
// Hours
case 'g':
sb.append(dt.getHourOfDay() % 12);
break;
case 'G':
sb.append(dt.getHourOfDay());
break;
case 'h':
sb.append(dt.getHourOfDay() < 10 ? "0" + dt.getHourOfDay() % 12 : dt.getHourOfDay() % 12);
break;
case 'H':
sb.append(dt.getHourOfDay() < 10 ? "0" + dt.getHourOfDay() : dt.getHourOfDay());
break;
// Minute
case 'i':
sb.append(dt.getMinuteOfHour() < 10 ? "0" + dt.getMinuteOfHour() : dt.getMinuteOfHour());
break;
// Second
case 's':
sb.append(dt.getSecondOfMinute());
break;
// Timezone
case 'e':
sb.append(dt.getZone());
break;
// Any other character
default:
sb.append(charArray[pos]);
}
pos++;
}
return sb.toString();
}
}