Java Code Examples for org.joda.time.DateTime#toDate()

The following examples show how to use org.joda.time.DateTime#toDate() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void testJodaLT() throws Exception {
    PropertyMapping<?, ?, DatastaxColumnKey> pm = newPM(org.joda.time.LocalTime.class, DataType.time());
    Setter<SettableByIndexData, org.joda.time.LocalTime> setter = factory.getSetter(pm);

    org.joda.time.LocalTime ldt = org.joda.time.LocalTime.fromMillisOfDay(3600000);

    setter.set(statement, ldt);
    setter.set(statement, null);

    verify(statement).setTime(0, TimeUnit.SECONDS.toNanos(3600));
    verify(statement).setToNull(0);

    final Date date = ldt.toDateTimeToday().toDate();
    DateTimeZone tz2 = getNonDefaultDateTimeZone(date);
    DateTime dateTime = ldt.toDateTimeToday(tz2);
    final Date dateTz = dateTime.toDate();

    Setter<SettableByIndexData, org.joda.time.LocalTime> setterTZ =
            factory.getSetter(newPM(org.joda.time.LocalTime.class, DataType.timestamp(), new JodaDateTimeZoneProperty(tz2)));
    setterTZ.set(statement, ldt);
    verify(statement).setTimestamp(1, new Date(dateTz.getTime()));

}
 
Example 2
Source Project: canal   File: DateUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 通用日期时间字符解析
 *
 * @param datetimeStr 日期时间字符串
 * @return Date
 */
public static Date parseDate(String datetimeStr) {
    if (StringUtils.isEmpty(datetimeStr)) {
        return null;
    }
    datetimeStr = datetimeStr.trim().replace('/', '-');
    if (datetimeStr.contains("-")) {
        if (datetimeStr.contains(":")) {
            datetimeStr = datetimeStr.replace(" ", "T");
        }
    } else if (datetimeStr.contains(":")) {
        datetimeStr = "T" + datetimeStr;
    } else {
        if (datetimeStr.length() == 8) {
            String year = datetimeStr.substring(0, 4);
            String month = datetimeStr.substring(4, 6);
            String day = datetimeStr.substring(6, 8);
            datetimeStr = year + "-" + month + "-" + day;
        }
    }

    DateTime dateTime = new DateTime(datetimeStr, dateTimeZone);

    return dateTime.toDate();
}
 
Example 3
@Test
public void messageformatter_with_choice() {
	
	String pattern = "Your statement on {0, date, MM/dd/yyyy} "
			+ "{1,choice,0#is not available|1#is {1, number, currency}}";
	
	MessageFormat statementFormat = new MessageFormat(pattern);
	
	DateTime statementDate1 = new DateTime(2013, 5, 15, 0, 0, 0, 0);

	Object[] statement1 = {statementDate1.toDate(), 23444};
	assertEquals("Your statement on  05/15/2013 is $23,444.00", 
			statementFormat.format(statement1));
	
	DateTime statementDate2 = new DateTime(2013, 6, 15, 0, 0, 0, 0);
	Object[] statement2 = {statementDate2.toDate(), 0};
	assertEquals("Your statement on  06/15/2013 is not available", 
			statementFormat.format(statement2));
}
 
Example 4
@Test
public void current_quarter () {
	
	DateTime firstQuarterCheck = new DateTime(2013, 1, 15, 0, 0, 0, 0);
	
	CurrentQuarterGuava currentQuarterWithGuava = 
			new CurrentQuarterGuava(firstQuarterCheck.toDate());
	
	assertEquals(new Integer(1), 
			currentQuarterWithGuava.getQuarter());
}
 
Example 5
Source Project: liteflow   File: TaskVersionUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 通过任务版本获取对应的时间
 * @param version
 * @param timeUnit
 * @return
 */
public static Date getDateByVersion(String version, TimeUnit timeUnit){

    String versionExpression = timeUnit.getVersionExpression();
    DateTimeFormatter format = DateTimeFormat.forPattern(versionExpression);
    DateTime dateTime = DateTime.parse(version, format);
    return dateTime.toDate();

}
 
Example 6
Source Project: liteflow   File: HourTimeCalculator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Date getStartTime() {
    DateTime dateTime = new DateTime(time);
    dateTime = dateTime.withMinuteOfHour(CommonConstants.ZERO);
    dateTime = dateTime.withSecondOfMinute(CommonConstants.ZERO);
    return dateTime.toDate();
}
 
Example 7
Source Project: ResearchStack   File: ScheduleHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static Date nextSchedule(String cronString, Date lastExecution) {
    DateTime now = new DateTime(lastExecution);
    CronParser cronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    Cron cron = cronParser.parse(cronString);

    ExecutionTime executionTime = ExecutionTime.forCron(cron);
    DateTime nextExecution = executionTime.nextExecution(now);

    return nextExecution.toDate();
}
 
Example 8
/**
 * Creates a date.
 *
 * @param day the day of the year.
 * @return a date.
 */
public Date getDay( int day )
{
    DateTime dataTime = DateTime.now();
    dataTime = dataTime.withTimeAtStartOfDay();
    dataTime = dataTime.withDayOfYear( day );

    return dataTime.toDate();
}
 
Example 9
Source Project: rya   File: DateTimeRyaTypeResolver.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected String serializeData(String data) throws RyaTypeResolverException {
    try {
        DateTime dateTime = DateTime.parse(data, XMLDATETIME_PARSER);
        Date value = dateTime.toDate();
        return DATE_STRING_TYPE_ENCODER.encode(value);
    } catch (TypeEncodingException e) {
        throw new RyaTypeResolverException(
                "Exception occurred serializing data[" + data + "]", e);
    }
}
 
Example 10
@Override
public void setUpTest()
{
    organisationUnit = createOrganisationUnit( 'A' );
    organisationUnitService.addOrganisationUnit( organisationUnit );

    OrganisationUnit organisationUnitB = createOrganisationUnit( 'B' );
    organisationUnitService.addOrganisationUnit( organisationUnitB );

    entityInstanceAttribute = createTrackedEntityAttribute( 'A' );
    attributeService.addTrackedEntityAttribute( entityInstanceAttribute );

    entityInstanceA1 = createTrackedEntityInstance( organisationUnit );
    entityInstanceB1 = createTrackedEntityInstance( organisationUnit );
    entityInstanceB1.setUid( "UID-B1" );

    programA = createProgram( 'A', new HashSet<>(), organisationUnit );

    programService.addProgram( programA );

    ProgramStage stageA = createProgramStage( 'A', programA );
    stageA.setSortOrder( 1 );
    programStageService.saveProgramStage( stageA );

    Set<ProgramStage> programStages = new HashSet<>();
    programStages.add( stageA );
    programA.setProgramStages( programStages );
    programService.updateProgram( programA );

    DateTime enrollmentDate = DateTime.now();
    enrollmentDate.withTimeAtStartOfDay();
    enrollmentDate = enrollmentDate.minusDays( 70 );

    DateTime incidenDate = DateTime.now();
    incidenDate.withTimeAtStartOfDay();

    programInstanceA = new ProgramInstance( enrollmentDate.toDate(), incidenDate.toDate(), entityInstanceA1,
        programA );
    programInstanceA.setUid( "UID-A" );
    programInstanceA.setOrganisationUnit( organisationUnit );

    programStageInstanceA = new ProgramStageInstance( programInstanceA, stageA );
    programInstanceA.setUid( "UID-PSI-A" );
    programInstanceA.setOrganisationUnit( organisationUnit );
}
 
Example 11
Source Project: DrivingAgency   File: DateTimeUtil.java    License: MIT License 4 votes vote down vote up
public static Date strToDate(String dateTimeStr){
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
    DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
    return dateTime.toDate();
}
 
Example 12
Source Project: liteflow   File: MinuteTimeCalculator.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Date getStartTime() {
    DateTime dateTime = new DateTime(time);
    dateTime = dateTime.withSecondOfMinute(CommonConstants.ZERO);
    return dateTime.toDate();
}
 
Example 13
Source Project: maven-framework-project   File: Jodatime.java    License: MIT License 4 votes vote down vote up
/**
 * 与jdk的时间转换
 */
public static void convertJDK(){
	
	System.out.println("-------------与jdk的时间转换-----------------");
	
	DateTime dt = new DateTime();

	//转换成java.util.Date对象
	
	Date d1 = new Date(dt.getMillis());
	
	Date d2 = dt.toDate();
	
	//转换成java.util.Calendar对象
	Calendar c1 = Calendar.getInstance();
	c1.setTimeInMillis(dt.getMillis());
	
	Calendar c2 = dt.toCalendar(Locale.getDefault());

}
 
Example 14
public Date convert(String source) {
    if (StringUtil.isEmpty(source)) {
        return null;
    }

    source = source.trim();
    if (StringUtil.isNotEmpty(source)){
        int length = source.length();
        if (length==10){
            if (UNIX_PATTERN.matcher(source).find()){
                return new Date(Long.parseLong(source)*1000);
            }
        }
        
        if (length==13){
            if (UNIX_PATTERN_MILLS.matcher(source).find()){
                return new Date(Long.parseLong(source));
            }
        }

        for (Entry<Pattern, SimpleDateFormat> dateEntry : DATE_PATTERN_FORMAT_MAP.entrySet()) {
            Pattern datePattern = dateEntry.getKey();
            Matcher dateMatcher = datePattern.matcher(source);
            if (dateMatcher.find()) {
                String dateStr = dateMatcher.group();
                try {
                    SimpleDateFormat yearFormat = dateEntry.getValue();
                    Date date = yearFormat.parse(dateStr);
                    DateTime dateTime = new DateTime(date);
                    String timeStr = source.substring(dateMatcher.end());
                    dateTime = parserMills(dateTime, timeStr, 0, TIME_PATTERN_FORMAT_MAP, MIC_PATTERN_FORMAT_MAP);
                    return dateTime.toDate();
                } catch (ParseException e) {
                    logger.error("", e);
                }
                break;
            }
        }
    }
    return null;
}
 
Example 15
Source Project: java-platform   File: BizEntity.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void setCreatedDate(final DateTime createdDate) {
	this.createdDate = null == createdDate ? null : createdDate.toDate();
}
 
Example 16
@Override
public Date convert(DateTime source) {
	return source.toDate();
}
 
Example 17
/**
 * Takes in an ISO date string of the following format:
 * yyyy-mm-ddThh:mm:ss.ms+HoMo
 *
 * @param isoDateString the iso date string
 * @return the date
 */
public static Date fromISODateString(String isoDateString) {

    DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
    DateTime dateTime = DateTime.parse(isoDateString, parser);
    return dateTime.toDate();
}
 
Example 18
Source Project: AsuraFramework   File: DateCalculator.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 获取指定日期偏移多少周后的此周第一天
 * 注意:周一是一周的第一天
 *
 * @param date 指定日期为标准
 * @param week 偏移周数,正数:往后指定周数量,负数:往前指定周数量
 * @return
 */
public static Date getFirstDayOfWeek(@NotNull Date date, int week) {
    Objects.requireNonNull(date, "date must not null");
    DateTime dateTime = new DateTime(date);
    dateTime = dateTime.plusWeeks(week).dayOfWeek().withMinimumValue();
    return dateTime.toDate();
}
 
Example 19
@Test
public void current_quarter_begin_date () {

	DateTime firstQuarterCheck = new DateTime(2013, 1, 15, 0, 0, 0, 0);

	CurrentQuarterGuava currentQuarterWithGuava = 
			new CurrentQuarterGuava(firstQuarterCheck.toDate());
	
	assertEquals(quarterBeginDate.toDate(), 
			currentQuarterWithGuava.getQuarterBeginDate());

}
 
Example 20
Source Project: Mycat2   File: DateUtil.java    License: GNU General Public License v3.0 2 votes vote down vote up
/**
 * 根据日期字符串和日期格式解析得到date类型日期
 * @param dateStr
 * @param datePattern
 * @return
 * @throws ParseException
 */
public static Date parseDate(String dateStr, String datePattern) throws ParseException {
	DateTime dt = DateTimeFormat.forPattern(datePattern).parseDateTime(dateStr);
	return dt.toDate();
}