Source Code Cross Referenced for Seconds.java in  » Development » Joda-Time » org » joda » time » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » Joda Time » org.joda.time 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2001-2006 Stephen Colebourne
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.joda.time;
017:
018:        import org.joda.time.base.BaseSingleFieldPeriod;
019:        import org.joda.time.field.FieldUtils;
020:        import org.joda.time.format.ISOPeriodFormat;
021:        import org.joda.time.format.PeriodFormatter;
022:
023:        /**
024:         * An immutable time period representing a number of seconds.
025:         * <p>
026:         * <code>Seconds</code> is an immutable period that can only store seconds.
027:         * It does not store years, months or hours for example. As such it is a
028:         * type-safe way of representing a number of seconds in an application.
029:         * <p>
030:         * The number of seconds is set in the constructor, and may be queried using
031:         * <code>getSeconds()</code>. Basic mathematical operations are provided -
032:         * <code>plus()</code>, <code>minus()</code>, <code>multipliedBy()</code> and
033:         * <code>dividedBy()</code>.
034:         * <p>
035:         * <code>Seconds</code> is thread-safe and immutable.
036:         *
037:         * @author Stephen Colebourne
038:         * @since 1.4
039:         */
040:        public final class Seconds extends BaseSingleFieldPeriod {
041:
042:            /** Constant representing zero seconds. */
043:            public static final Seconds ZERO = new Seconds(0);
044:            /** Constant representing one second. */
045:            public static final Seconds ONE = new Seconds(1);
046:            /** Constant representing two seconds. */
047:            public static final Seconds TWO = new Seconds(2);
048:            /** Constant representing three seconds. */
049:            public static final Seconds THREE = new Seconds(3);
050:            /** Constant representing the maximum number of seconds that can be stored in this object. */
051:            public static final Seconds MAX_VALUE = new Seconds(
052:                    Integer.MAX_VALUE);
053:            /** Constant representing the minimum number of seconds that can be stored in this object. */
054:            public static final Seconds MIN_VALUE = new Seconds(
055:                    Integer.MIN_VALUE);
056:
057:            /** The paser to use for this class. */
058:            private static final PeriodFormatter PARSER = ISOPeriodFormat
059:                    .standard().withParseType(PeriodType.seconds());
060:            /** Serialization version. */
061:            private static final long serialVersionUID = 87525275727380862L;
062:
063:            //-----------------------------------------------------------------------
064:            /**
065:             * Obtains an instance of <code>Seconds</code> that may be cached.
066:             * <code>Seconds</code> is immutable, so instances can be cached and shared.
067:             * This factory method provides access to shared instances.
068:             *
069:             * @param seconds  the number of seconds to obtain an instance for
070:             * @return the instance of Seconds
071:             */
072:            public static Seconds seconds(int seconds) {
073:                switch (seconds) {
074:                case 0:
075:                    return ZERO;
076:                case 1:
077:                    return ONE;
078:                case 2:
079:                    return TWO;
080:                case 3:
081:                    return THREE;
082:                case Integer.MAX_VALUE:
083:                    return MAX_VALUE;
084:                case Integer.MIN_VALUE:
085:                    return MIN_VALUE;
086:                default:
087:                    return new Seconds(seconds);
088:                }
089:            }
090:
091:            //-----------------------------------------------------------------------
092:            /**
093:             * Creates a <code>Seconds</code> representing the number of whole seconds
094:             * between the two specified datetimes.
095:             *
096:             * @param start  the start instant, must not be null
097:             * @param end  the end instant, must not be null
098:             * @return the period in seconds
099:             * @throws IllegalArgumentException if the instants are null or invalid
100:             */
101:            public static Seconds secondsBetween(ReadableInstant start,
102:                    ReadableInstant end) {
103:                int amount = BaseSingleFieldPeriod.between(start, end,
104:                        DurationFieldType.seconds());
105:                return Seconds.seconds(amount);
106:            }
107:
108:            /**
109:             * Creates a <code>Seconds</code> representing the number of whole seconds
110:             * between the two specified partial datetimes.
111:             * <p>
112:             * The two partials must contain the same fields, for example you can specify
113:             * two <code>LocalTime</code> objects.
114:             *
115:             * @param start  the start partial date, must not be null
116:             * @param end  the end partial date, must not be null
117:             * @return the period in seconds
118:             * @throws IllegalArgumentException if the partials are null or invalid
119:             */
120:            public static Seconds secondsBetween(ReadablePartial start,
121:                    ReadablePartial end) {
122:                if (start instanceof  LocalTime && end instanceof  LocalTime) {
123:                    Chronology chrono = DateTimeUtils.getChronology(start
124:                            .getChronology());
125:                    int seconds = chrono.seconds().getDifference(
126:                            ((LocalTime) end).getLocalMillis(),
127:                            ((LocalTime) start).getLocalMillis());
128:                    return Seconds.seconds(seconds);
129:                }
130:                int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
131:                return Seconds.seconds(amount);
132:            }
133:
134:            /**
135:             * Creates a <code>Seconds</code> representing the number of whole seconds
136:             * in the specified interval.
137:             *
138:             * @param interval  the interval to extract seconds from, null returns zero
139:             * @return the period in seconds
140:             * @throws IllegalArgumentException if the partials are null or invalid
141:             */
142:            public static Seconds secondsIn(ReadableInterval interval) {
143:                if (interval == null) {
144:                    return Seconds.ZERO;
145:                }
146:                int amount = BaseSingleFieldPeriod.between(interval.getStart(),
147:                        interval.getEnd(), DurationFieldType.seconds());
148:                return Seconds.seconds(amount);
149:            }
150:
151:            /**
152:             * Creates a new <code>Seconds</code> representing the number of complete
153:             * standard length seconds in the specified period.
154:             * <p>
155:             * This factory method converts all fields from the period to hours using standardised
156:             * durations for each field. Only those fields which have a precise duration in
157:             * the ISO UTC chronology can be converted.
158:             * <ul>
159:             * <li>One week consists of 7 seconds.
160:             * <li>One day consists of 24 hours.
161:             * <li>One hour consists of 60 minutes.
162:             * <li>One minute consists of 60 seconds.
163:             * <li>One second consists of 1000 milliseconds.
164:             * </ul>
165:             * Months and Years are imprecise and periods containing these values cannot be converted.
166:             *
167:             * @param period  the period to get the number of hours from, null returns zero
168:             * @return the period in seconds
169:             * @throws IllegalArgumentException if the period contains imprecise duration values
170:             */
171:            public static Seconds standardSecondsIn(ReadablePeriod period) {
172:                int amount = BaseSingleFieldPeriod.standardPeriodIn(period,
173:                        DateTimeConstants.MILLIS_PER_SECOND);
174:                return Seconds.seconds(amount);
175:            }
176:
177:            /**
178:             * Creates a new <code>Seconds</code> by parsing a string in the ISO8601 format 'PTnS'.
179:             * <p>
180:             * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
181:             * seconds component may be non-zero. If any other component is non-zero, an exception
182:             * will be thrown.
183:             *
184:             * @param periodStr  the period string, null returns zero
185:             * @return the period in seconds
186:             * @throws IllegalArgumentException if the string format is invalid
187:             */
188:            public static Seconds parseSeconds(String periodStr) {
189:                if (periodStr == null) {
190:                    return Seconds.ZERO;
191:                }
192:                Period p = PARSER.parsePeriod(periodStr);
193:                return Seconds.seconds(p.getSeconds());
194:            }
195:
196:            //-----------------------------------------------------------------------
197:            /**
198:             * Creates a new instance representing a number of seconds.
199:             * You should consider using the factory method {@link #seconds(int)}
200:             * instead of the constructor.
201:             *
202:             * @param seconds  the number of seconds to represent
203:             */
204:            private Seconds(int seconds) {
205:                super (seconds);
206:            }
207:
208:            /**
209:             * Resolves singletons.
210:             * 
211:             * @return the singleton instance
212:             */
213:            private Object readResolve() {
214:                return Seconds.seconds(getValue());
215:            }
216:
217:            //-----------------------------------------------------------------------
218:            /**
219:             * Gets the duration field type, which is <code>seconds</code>.
220:             *
221:             * @return the period type
222:             */
223:            public DurationFieldType getFieldType() {
224:                return DurationFieldType.seconds();
225:            }
226:
227:            /**
228:             * Gets the period type, which is <code>seconds</code>.
229:             *
230:             * @return the period type
231:             */
232:            public PeriodType getPeriodType() {
233:                return PeriodType.seconds();
234:            }
235:
236:            //-----------------------------------------------------------------------
237:            /**
238:             * Converts this period in seconds to a period in weeks assuming a
239:             * 7 day week, 24 hour day, 60 minute hour and 60 second minute.
240:             * <p>
241:             * This method allows you to convert between different types of period.
242:             * However to achieve this it makes the assumption that all weeks are 7 days
243:             * long, all days are 24 hours long, all hours are 60 minutes long and
244:             * all minutes are 60 seconds long.
245:             * This is not true when daylight savings time is considered, and may also
246:             * not be true for some unusual chronologies. However, it is included as it
247:             * is a useful operation for many applications and business rules.
248:             * 
249:             * @return a period representing the number of whole weeks for this number of seconds
250:             */
251:            public Weeks toStandardWeeks() {
252:                return Weeks.weeks(getValue()
253:                        / DateTimeConstants.SECONDS_PER_WEEK);
254:            }
255:
256:            /**
257:             * Converts this period in seconds to a period in days assuming a
258:             * 24 hour day, 60 minute hour and 60 second minute.
259:             * <p>
260:             * This method allows you to convert between different types of period.
261:             * However to achieve this it makes the assumption that all days are 24 hours
262:             * long, all hours are 60 minutes long and all minutes are 60 seconds long.
263:             * This is not true when daylight savings is considered and may also not
264:             * be true for some unusual chronologies. However, it is included
265:             * as it is a useful operation for many applications and business rules.
266:             * 
267:             * @return a period representing the number of days for this number of seconds
268:             */
269:            public Days toStandardDays() {
270:                return Days
271:                        .days(getValue() / DateTimeConstants.SECONDS_PER_DAY);
272:            }
273:
274:            /**
275:             * Converts this period in seconds to a period in hours assuming a
276:             * 60 minute hour and 60 second minute.
277:             * <p>
278:             * This method allows you to convert between different types of period.
279:             * However to achieve this it makes the assumption that all hours are
280:             * 60 minutes long and all minutes are 60 seconds long.
281:             * This may not be true for some unusual chronologies. However, it is included
282:             * as it is a useful operation for many applications and business rules.
283:             * 
284:             * @return a period representing the number of hours for this number of seconds
285:             */
286:            public Hours toStandardHours() {
287:                return Hours.hours(getValue()
288:                        / DateTimeConstants.SECONDS_PER_HOUR);
289:            }
290:
291:            /**
292:             * Converts this period in seconds to a period in minutes assuming a
293:             * 60 second minute.
294:             * <p>
295:             * This method allows you to convert between different types of period.
296:             * However to achieve this it makes the assumption that all minutes are
297:             * 60 seconds long.
298:             * This may not be true for some unusual chronologies. However, it is included
299:             * as it is a useful operation for many applications and business rules.
300:             * 
301:             * @return a period representing the number of minutes for this number of seconds
302:             */
303:            public Minutes toStandardMinutes() {
304:                return Minutes.minutes(getValue()
305:                        / DateTimeConstants.SECONDS_PER_MINUTE);
306:            }
307:
308:            //-----------------------------------------------------------------------
309:            /**
310:             * Converts this period in seconds to a duration in milliseconds assuming a
311:             * 24 hour day, 60 minute hour and 60 second minute.
312:             * <p>
313:             * This method allows you to convert from a period to a duration.
314:             * However to achieve this it makes the assumption that all seconds are 24 hours
315:             * long, all hours are 60 minutes and all minutes are 60 seconds.
316:             * This is not true when daylight savings time is considered, and may also
317:             * not be true for some unusual chronologies. However, it is included as it
318:             * is a useful operation for many applications and business rules.
319:             * 
320:             * @return a duration equivalent to this number of seconds
321:             */
322:            public Duration toStandardDuration() {
323:                long seconds = getValue(); // assign to a long
324:                return new Duration(seconds
325:                        * DateTimeConstants.MILLIS_PER_SECOND);
326:            }
327:
328:            //-----------------------------------------------------------------------
329:            /**
330:             * Gets the number of seconds that this period represents.
331:             *
332:             * @return the number of seconds in the period
333:             */
334:            public int getSeconds() {
335:                return getValue();
336:            }
337:
338:            //-----------------------------------------------------------------------
339:            /**
340:             * Returns a new instance with the specified number of seconds added.
341:             * <p>
342:             * This instance is immutable and unaffected by this method call.
343:             *
344:             * @param seconds  the amount of seconds to add, may be negative
345:             * @return the new period plus the specified number of seconds
346:             * @throws ArithmeticException if the result overflows an int
347:             */
348:            public Seconds plus(int seconds) {
349:                if (seconds == 0) {
350:                    return this ;
351:                }
352:                return Seconds.seconds(FieldUtils.safeAdd(getValue(), seconds));
353:            }
354:
355:            /**
356:             * Returns a new instance with the specified number of seconds added.
357:             * <p>
358:             * This instance is immutable and unaffected by this method call.
359:             *
360:             * @param seconds  the amount of seconds to add, may be negative, null means zero
361:             * @return the new period plus the specified number of seconds
362:             * @throws ArithmeticException if the result overflows an int
363:             */
364:            public Seconds plus(Seconds seconds) {
365:                if (seconds == null) {
366:                    return this ;
367:                }
368:                return plus(seconds.getValue());
369:            }
370:
371:            //-----------------------------------------------------------------------
372:            /**
373:             * Returns a new instance with the specified number of seconds taken away.
374:             * <p>
375:             * This instance is immutable and unaffected by this method call.
376:             *
377:             * @param seconds  the amount of seconds to take away, may be negative
378:             * @return the new period minus the specified number of seconds
379:             * @throws ArithmeticException if the result overflows an int
380:             */
381:            public Seconds minus(int seconds) {
382:                return plus(FieldUtils.safeNegate(seconds));
383:            }
384:
385:            /**
386:             * Returns a new instance with the specified number of seconds taken away.
387:             * <p>
388:             * This instance is immutable and unaffected by this method call.
389:             *
390:             * @param seconds  the amount of seconds to take away, may be negative, null means zero
391:             * @return the new period minus the specified number of seconds
392:             * @throws ArithmeticException if the result overflows an int
393:             */
394:            public Seconds minus(Seconds seconds) {
395:                if (seconds == null) {
396:                    return this ;
397:                }
398:                return minus(seconds.getValue());
399:            }
400:
401:            //-----------------------------------------------------------------------
402:            /**
403:             * Returns a new instance with the seconds multiplied by the specified scalar.
404:             * <p>
405:             * This instance is immutable and unaffected by this method call.
406:             *
407:             * @param scalar  the amount to multiply by, may be negative
408:             * @return the new period multiplied by the specified scalar
409:             * @throws ArithmeticException if the result overflows an int
410:             */
411:            public Seconds multipliedBy(int scalar) {
412:                return Seconds.seconds(FieldUtils.safeMultiply(getValue(),
413:                        scalar));
414:            }
415:
416:            /**
417:             * Returns a new instance with the seconds divided by the specified divisor.
418:             * The calculation uses integer division, thus 3 divided by 2 is 1.
419:             * <p>
420:             * This instance is immutable and unaffected by this method call.
421:             *
422:             * @param divisor  the amount to divide by, may be negative
423:             * @return the new period divided by the specified divisor
424:             * @throws ArithmeticException if the divisor is zero
425:             */
426:            public Seconds dividedBy(int divisor) {
427:                if (divisor == 1) {
428:                    return this ;
429:                }
430:                return Seconds.seconds(getValue() / divisor);
431:            }
432:
433:            //-----------------------------------------------------------------------
434:            /**
435:             * Returns a new instance with the seconds value negated.
436:             *
437:             * @return the new period with a negated value
438:             * @throws ArithmeticException if the result overflows an int
439:             */
440:            public Seconds negated() {
441:                return Seconds.seconds(FieldUtils.safeNegate(getValue()));
442:            }
443:
444:            //-----------------------------------------------------------------------
445:            /**
446:             * Is this seconds instance greater than the specified number of seconds.
447:             *
448:             * @param other  the other period, null means zero
449:             * @return true if this seconds instance is greater than the specified one
450:             */
451:            public boolean isGreaterThan(Seconds other) {
452:                if (other == null) {
453:                    return getValue() > 0;
454:                }
455:                return getValue() > other.getValue();
456:            }
457:
458:            /**
459:             * Is this seconds instance less than the specified number of seconds.
460:             *
461:             * @param other  the other period, null means zero
462:             * @return true if this seconds instance is less than the specified one
463:             */
464:            public boolean isLessThan(Seconds other) {
465:                if (other == null) {
466:                    return getValue() < 0;
467:                }
468:                return getValue() < other.getValue();
469:            }
470:
471:            //-----------------------------------------------------------------------
472:            /**
473:             * Gets this instance as a String in the ISO8601 duration format.
474:             * <p>
475:             * For example, "PT4S" represents 4 seconds.
476:             *
477:             * @return the value as an ISO8601 string
478:             */
479:            public String toString() {
480:                return "PT" + String.valueOf(getValue()) + "S";
481:            }
482:
483:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.