Java 如何从日历中获取日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3574811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get a Date from my Calendar?
提问by Twity
I have a Map
containing the birthdate of a person as a GregorianCalendar
.
我有一个Map
包含一个人的生日的GregorianCalendar
.
For example:
例如:
{
motherEmailID=null,
coreType=Ticket,
_NULL=null,
additionalFaclitiesProvided=[],
dateOfBirth=java.util.GregorianCalendar[
time=585340200000,
areFieldsSet=false,
areAllFieldsSet=false,
lenient=true,
zone=sun.util.calendar.ZoneInfo[id="GMT",
offset=0,
dstSavings=0,
useDaylight=false,
transitions=0,
lastRule=null],
firstDayOfWeek=1,
minimalDaysInFirstWeek=1,
ERA=1,
YEAR=1988,
MONTH=6,
WEEK_OF_YEAR=30,
WEEK_OF_MONTH=4,
DAY_OF_MONTH=20,
DAY_OF_YEAR=202,
DAY_OF_WEEK=4,
DAY_OF_WEEK_IN_MONTH=3,
AM_PM=0,
HOUR=0,
HOUR_OF_DAY=0,
MINUTE=0,
SECOND=0,
MILLISECOND=0,
ZONE_OFFSET=19800000,
DST_OFFSET=0],
targetEnd=null,
year_semester=null
}
I need a Date
, but in my database it is in Calendar
format only.
The datatype of column in the database is DateTime
.
How can I get the birthdate in a Date
format?
我需要一个Date
,但在我的数据库中它只是Calendar
格式。数据库中列的数据类型是DateTime
. 如何以Date
格式获取出生日期?
回答by OscarRyz
Calendar calendar = ( Calendar ) thatMap.get("dateOfBirth");
Date date = calendar.getTime();
Here's a sample you can use to test it, and see it does what you need.
这是您可以用来测试它的示例,并查看它是否满足您的需求。
import java.util.*;
import java.text.*;
public class GetDate {
public static void main( String [] args ) {
Map map = new HashMap();
map.put("dateOfBirth", Calendar.getInstance() );
map.put("additionalFaclitiesProvided", new ArrayList() );
/// etc.
System.out.println( map );
Calendar cal = ( Calendar ) map.get("dateOfBirth");
Date date = cal.getTime();
// Addressing your comment:
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
System.out.println( "The date is: "+ sdf.format( date ) );
}
}
Output:
输出:
java GetDate
{dateOfBirth=java.util.GregorianCalendar[
time=1282824447050,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[
id="America/Mexico_City",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=99,lastRule=java.util.SimpleTimeZone[
id=America/Mexico_City,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1,
startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0
]
],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=7,WEEK_OF_YEAR=35,WEEK_OF_MONTH=4,DAY_OF_MONTH=26,DAY_OF_YEAR=238,DAY_OF_WEEK=5,
DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=7,HOUR_OF_DAY=7,MINUTE=7,SECOND=27,MILLISECOND=50,ZONE_OFFSET=-21600000,DST_OFFSET=3600000]**, additionalFaclitiesProvided=[]
}
The date is: 26.08.2010
日期是:26.08.2010
回答by Yanick Rochon
From java.sql.Date
to java.util.Calendar
(or java.util.GregorianCalendar
)
从java.sql.Date
到java.util.Calendar
(或java.util.GregorianCalendar
)
Calendar cal = new GregorianCalendar();
cal.setTime(date); // java.sql.Date date;
// then set the GregorianCalendar in your map
map.put('dateOfBirth', cal);
From java.util.Calendar
to java.sql.Date
从java.util.Calendar
到java.sql.Date
java.sql.Date date = new java.sql.Date(map.get('dateOfBirth').getTimeInMillis());
** NOTE**
**注意**
java.sql.Timestamp
is a sibling of java.sql.Date
and both extends java.util.Date
, therefore you can use either exactly the same way.
java.sql.Timestamp
是java.sql.Date
和都 extends的兄弟java.util.Date
,因此您可以使用完全相同的方式。
Also, to convert a date string into a date object, use SimpleDateFormat:
此外,要将日期字符串转换为日期对象,请使用SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date d = sdf.parse("2010-08-26 8:34:00");
Calendar cal = Calendar.getInstance();
cal.setTime(d);
And to reverse it
并扭转它
String dateStr1 = sdf.format(cal.getTime());
// or
String dateStr2 = sdf.format(date); // java.sql.Date / java.util.Date
回答by Jaffar Ramay
Copy and run the example.
复制并运行示例。
Just change mode to test different switch conditions.
只需更改模式即可测试不同的开关条件。
For encapsulation just made couple of methods private. Date formatting isn't your job to do because method is now returning date and it is up to the caller to use what ever format he wants.
对于封装,只是将几个方法设为私有。日期格式不是你的工作,因为方法现在返回日期,调用者可以使用他想要的任何格式。
import java.util.Date;
import java.util.GregorianCalendar;
public class RandomDateOfBirthGenerator {
private static GregorianCalendar gc = new GregorianCalendar();
public static enum Mode {
SENIOR, ADULT, YOUTH, CHILD
}
public static Date generateRandomDateOfBirth(Mode mode) {
int year = 0;
switch(mode){
case SENIOR:
year = randBetween(1900, 1940);
break;
case ADULT:
year = randBetween(1941, 1995);
break;
case YOUTH:
year = randBetween(1995, 2002);
break;
case CHILD:
year = randBetween(2002, 2014);
break;
}
gc.set(gc.YEAR, year);
int dayOfYear = randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR));
gc.set(gc.DAY_OF_YEAR, dayOfYear);
return gc.getTime();
}
private static int randBetween(int start, int end) {
return start + (int) Math.round(Math.random() * (end - start));
}
public static void main(String[] args) {
System.out.println(generateRandomDateOfBirth(Mode.CHILD));
}
}