Revision: 4897 http://sourceforge.net/p/vexi/code/4897 Author: mkpg2 Date: 2016-11-02 02:12:53 +0000 (Wed, 02 Nov 2016) Log Message: -----------
Modified Paths: -------------- branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java Modified: branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java =================================================================== --- branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java 2016-11-02 02:07:56 UTC (rev 4896) +++ branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java 2016-11-02 02:12:53 UTC (rev 4897) @@ -8,6 +8,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; + /** * <p>Represents a date. Using the gregorian calendar.</p> * @@ -63,7 +64,7 @@ PART_WEEKDAY = -6, PART_WEEK = -7, PART_WEEKYEAR = -8; - + static public int partId(String partName) throws ValueException{ if("year".equals(partName)) return PART_YEAR; if("month".equals(partName)) return PART_MONTH; @@ -142,7 +143,7 @@ static public class Scheme{ final public String name; - final int[] partIds; + final public int[] partIds; Scheme(String name, int[] partIds){ this.name = name; this.partIds = partIds; @@ -162,6 +163,12 @@ vs[index] = value; } + public Date newDate(java.util.Date d) { + if(d==null) return null; + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(d.getTime()); + return newDate(c); + } public Date newDate(Calendar cal) { cal = initCalendar(cal); @@ -207,11 +214,11 @@ // } return c; } - + public int getMinorPart() { return partIds[partIds.length-1]; } - + public Date tryParseString(String s){ String s1 = s.replaceAll("[^\\d]+", "/"); String[] partStrs = s1.split("/"); @@ -234,7 +241,8 @@ parts[i] = partValue; } return new Date(this, parts); - } + } + } @@ -283,8 +291,8 @@ static Pattern p = Pattern.compile("([0-9]{1,4})((wk)|[q-])?([0-9]{1,2})?(-([0-9]{1,2})( ([0-9]{1,2})(:([0-9]{1,2}))?)?)?"); static public Date parseString(String s) throws ValueException{ - if(s==null) return null; - Date r = tryParseString(s); + if(s==null) return null; + Date r = tryParseString(s); if(r==null) throw new ValueException("Not a date '" + s + "'"); return r; @@ -540,7 +548,6 @@ return s; } - private Calendar asCalendar() { if(hasPart(PART_WEEK)){ @@ -556,10 +563,11 @@ getPartDefault(PART_MONTH)-1, getPartDefault(PART_DAY), getPartDefault(PART_HOUR), - getPartDefault(PART_MINUTE)) - ); + getPartDefault(PART_MINUTE) + )); } } + public long asMillisUTC() { return asMillis(TimeZone.getTimeZone("UTC")); } public long asMillis(TimeZone timezone) { Calendar cal = asCalendar(); @@ -618,6 +626,7 @@ c.set(Calendar.DAY_OF_WEEK, value); return SCHEME_YMD.newDate(c); }else{ + Scheme scheme1 = getSchemeForPart(part); int[] r = new int[parts.length+1]; System.arraycopy(parts, 0, r, 0, parts.length); @@ -653,44 +662,44 @@ return scheme.newDate(c); } - public Date addPeriod(int part, int amount) throws ValueException{ - Calendar c = asCalendar(); - if(hasPart(part)){ - switch(part){ - case PART_MINUTE: - c.add(Calendar.MINUTE, amount); - break; - case PART_HOUR: - c.add(Calendar.HOUR_OF_DAY, amount); - break; - case PART_YEAR: - c.add(Calendar.YEAR, amount); - break; - case PART_QUARTER: - c.add(Calendar.MONTH, amount*3); - break; - case PART_MONTH: - c.add(Calendar.MONTH, amount); - break; - case PART_DAY: - c.add(Calendar.DAY_OF_MONTH, amount); - break; - case PART_WEEK: - c.add(Calendar.WEEK_OF_YEAR, amount); - break; - default: + public Date addPeriod(int part, int amount) throws ValueException{ + Calendar c = asCalendar(); + if(hasPart(part)) { + switch(part){ + case PART_MINUTE: + c.add(Calendar.MINUTE, amount); + break; + case PART_HOUR: + c.add(Calendar.HOUR_OF_DAY, amount); + break; + case PART_YEAR: + c.add(Calendar.YEAR, amount); + break; + case PART_QUARTER: + c.add(Calendar.MONTH, amount*3); + break; + case PART_MONTH: + c.add(Calendar.MONTH, amount); + break; + case PART_DAY: + c.add(Calendar.DAY_OF_MONTH, amount); + break; + case PART_WEEK: + c.add(Calendar.WEEK_OF_YEAR, amount); + break; + default: throw new ValueException("Cannot add "+partName(part)+" to "+ scheme.name); - } + } }else{ if(part==PART_WEEK && hasPart(PART_DAY)){ c.add(Calendar.DAY_OF_MONTH, amount*7); }else{ throw new ValueException("Cannot add "+partName(part)+" to "+ scheme.name); - } + } } return correct(c, part); } - + private Date correct(Calendar c, int partStart) throws ValueException{ Date r = scheme.newDate(c); // check smaller parts as it is possible for these to drift (e.g. taking years from a week date) @@ -706,7 +715,6 @@ } return r; } - public int diff(int part, Date from) throws ValueException{ int years = getPart(PART_YEAR)-from.getPart(PART_YEAR); switch(part){ @@ -783,7 +791,7 @@ } while (cursor.getTimeInMillis() <= endInstant); return presumedDays; } - + static final double D_MULTIPL_HOUR = 60 * 60 * 1000; static final double D_MULTIPL_MIN = 60 * 1000; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Developer Access Program for Intel Xeon Phi Processors Access to Intel Xeon Phi processor-based developer platforms. With one year of Intel Parallel Studio XE. Training and support from Colfax. Order your platform today. http://sdm.link/xeonphi _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn