是否可以从字符串创建Java.time.LocalDate对象?
我一直在尝试使用date.format(DateTimeFormatter formatter)方法格式化日期字符串列表,例如,“date”是java.time.LocalDate对象。问题是,我找不到从字符串创建Year对象的直接方法。例如,如果我有字符串yearString=“90”。我想创建一个等于此值的Year对象,然后使用format方法输出yearStringNew=“1990”。最接近公共构造函数的是now()函数,它返回当前时间 我还研究了创建一个日历对象,然后在那里创建一个可格式化的日期对象,但我只能创建一个Java.util.date对象,而不是Java.time包中的对象,理想情况下,该对象可以通过format函数格式化。我是不是遗漏了什么 仅供参考,我引用的是Java8SDK javadoc 谢谢你抽出时间 编辑:根据其他用户的请求,我已在下面发布了我的代码;这是我最接近实现我所追求的目标的时刻:是否可以从字符串创建Java.time.LocalDate对象?,java,Java,我一直在尝试使用date.format(DateTimeFormatter formatter)方法格式化日期字符串列表,例如,“date”是java.time.LocalDate对象。问题是,我找不到从字符串创建Year对象的直接方法。例如,如果我有字符串yearString=“90”。我想创建一个等于此值的Year对象,然后使用format方法输出yearStringNew=“1990”。最接近公共构造函数的是now()函数,它返回当前时间 我还研究了创建一个日历对象,然后在那里创建一个可格
//Module 3:
//Format Date Segments
package challenge245E;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class TestClass3 {
public static void main(String[] args) throws ParseException {
DateFormatter dateFormatter = new DateFormatter();
String myGroupedSlices [][] =
{
{"1990", "12", "06"},
{"12","6", "90"}
};
dateFormatter.formatDates(myGroupedSlices);
}
}
class DateFormatter {
public Date[][] formatDates(String[][] groupedDates) throws ParseException {
Date[][] formattedDates = new Date[groupedDates.length][3];
DateFormat yearFormat = new SimpleDateFormat("YYYY");
DateFormat monthFormat = new SimpleDateFormat("MM");
DateFormat dayFormat = new SimpleDateFormat("dd");
//iterate through each groupedSlices array
for (int i=0; i<groupedDates.length;i++) {
//Conditions
if (groupedDates[i][0].length()<3) {
//MDDYY format: if date[0].length < 3
//Re-arrange into YDM order
String m = groupedDates[i][0];
String y = groupedDates[i][2];
groupedDates[i][0] = y;
groupedDates[i][2] = m;
//convert dates to correct format
formattedDates[i][0] = yearFormat.parse(groupedDates[i][0]);
formattedDates[i][1] = monthFormat.parse(groupedDates[i][1]);
formattedDates[i][2] = dayFormat.parse(groupedDates[i][2]);
//testing if block
System.out.println("MDY Order: " + Arrays.toString(formattedDates[i]));
}
if (groupedDates[i][0].length()>3) {
//YYYYMMDD format: if date[0].length > 3
//convert dates to correct format
formattedDates[i][0] = yearFormat.parse(groupedDates[i][0]);
formattedDates[i][1] = monthFormat.parse(groupedDates[i][1]);
formattedDates[i][2] = dayFormat.parse(groupedDates[i][2]);
//testing if block
System.out.println("YMD Order: " + Arrays.toString(formattedDates[i]));
}
}
return formattedDates;
}
}
//模块3:
//格式化日期段
一揽子挑战245E;
导入java.text.DateFormat;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.array;
导入java.util.Date;
公共类TestClass3{
公共静态void main(字符串[]args)引发异常{
DateFormatter DateFormatter=新的DateFormatter();
字符串myGroupedSlices[]]=
{
{"1990", "12", "06"},
{"12","6", "90"}
};
dateFormatter.formatDates(myGroupedSlices);
}
}
类日期格式化程序{
公共日期[][]格式日期(字符串[][]groupedDates)引发异常{
日期[][]格式日期=新日期[groupedDates.length][3];
DateFormat yearFormat=新的SimpleDateFormat(“YYYY”);
DateFormat monthFormat=新的SimpleDateFormat(“MM”);
DateFormat dayFormat=新的SimpleDateFormat(“dd”);
//遍历每个groupedSlices数组
对于(int i=0;i 3
//将日期转换为正确的格式
formattedDates[i][0]=yearFormat.parse(groupedDates[i][0]);
formattedDates[i][1]=monthFormat.parse(groupedDates[i][1]);
formattedDates[i][2]=dayFormat.parse(groupedDates[i][2]);
//测试if块
System.out.println(“YMD顺序:+array.toString(formattedDates[i]));
}
}
返回格式化日期;
}
}
如果我正确理解您的需求,请查看方法
例如:
LocalDate date = LocalDate.parse("1990-01-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
int year = date.getYear(); // 1990
创建GregorianCalendar类的实例,在该日历中设置日期,然后使用方法toZonedDateTime()。这将为您提供ZonedDateTime实例。在该实例中,您可以使用方法LocalDate.from(TemporalAccessor temporal)方法获取LocalDate。其外观如下:
//....
GregorianCalendar calendar = new GregorianCalendar();
// Set the deasired date in your calendar
LocalDate localDate = LocalDate.from(calendar.toZonedDateTime());
//...
分别解析每个数字
很高兴看到您使用该框架而不是麻烦的旧日期时间类。旧的java.util.date/.Calendar类已被新框架取代
该类将任何两位数的年份解析为2000年代。从类文档:
如果字母数为2…将使用基值2000进行分析,结果是2000到2099(含)范围内的一年
若要覆盖此行为,请参见。但此问题在您的案例中可能没有意义。您已经隔离了单独的年、月和日期值。因此,它们不需要一起解析
我建议您将每个字符串转换为一个整数。对于年份,如果小于100,则添加1900
String inputYear = "90";
String inputMonth = "12";
String inputDayOfMonth = "6";
int year = Integer.parseInt( inputYear );
int month = Integer.parseInt( inputMonth );
int dayOfMonth = Integer.parseInt( inputDayOfMonth );
if( year < 100 ) { // If two-digit year, assume the 1900s century. Even better: Never generate two-digit year text!
year = ( year + 1900 );
}
LocalDate localDate = LocalDate.of( year , month , dayOfMonth );
String inputYear=“90”;
字符串inputMonth=“12”;
字符串inputDayOfMonth=“6”;
int year=Integer.parseInt(inputYear);
int month=Integer.parseInt(inputMonth);
int dayOfMonth=Integer.parseInt(inputDayOfMonth);
如果(年份<100){//如果是两位数的年份,假设是1900年代。更好的是:永远不要生成两位数的年份文本!
年份=(年份+1900);
}
LocalDate LocalDate=LocalDate.of(年、月、月);
导入java.time.LocalDate
公课日下个月{
public static void main(String[] args) {
//create an object for LocalDate class
LocalDate date = LocalDate.now();
//get today's day
int today = date.getDayOfMonth();
//get number of days in the current month
int numOfDaysInMonth = date.lengthOfMonth();
//compute the days left for next month
int dayForNextMonth = numOfDaysInMonth - today;
//display the result
System.out.println("The next month is: " + date.plusMonths(7).getMonth());
System.out.println("We have " + dayForNextMonth + " days left until first day of next month.");
}
}请参见,非常麻烦的旧日期时间类,例如,
java.text.SimpleDateFormat
现在被java 8和更高版本中内置的类所取代。请参见。我在尝试更早地解析这个特定日期时遇到了2000到2099年的问题——至少现在知道没有内置的方法来解析它是很有帮助的o绕开这一点。至于两位数年份——具体场景涉及格式化各种日期,其中一些可能是两位数年份——无论如何,我创建了一个类似于您在上面为此类案例编写的“如果”分支。感谢您的回答,这非常有帮助。如果我有特权,我会投票吗。@BenWS有一种内置的解决方法。请参见我的链接。使用DateTimeFormatterBuilder
并调用appendValueReduced
。感谢您的回答–我在格式化年份日期的ofPattern方法方面遇到了一些问题(“90”而不是“90-01-12”),但我已经找到了解决这个问题的方法。下次我必须更仔细地研究类方法。再次感谢!如果我有特权,我会投票的。