Intermediate
Date of registration: Jan 1st 2009
Location: Hanover, Germany
Occupation: Software Engineer
Hobbies: Hilbert Curves
Not sure, which is at this point the best.
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import VBA.*; import java.text.*; import java.util.*; public class DateTime { public static long DateDiff(String Interval, VBVariant Date1, VBVariant Date2) { // Gibt die Anzahl der Zeitintervalle zwischen zwei Datumsangaben zurück // returns the amount of timespans between the two dates // Interval: a string representing the kind of time // Date1: one end of the timespan // Date2: the other end of the timespan // note: does not consider different first days of the week return DateVal(Interval, Date2) - DateVal(Interval, Date1); } private static long DateVal(String Interval, VBVariant d) { Date d1 = (Date)d.toObject(); long sec = Conversion.CLng(VBVariant.valueOf(d1.getTime() * 0.001)); int y = d1.getYear(); long ret = 0; if (Interval.equals("s")) ret = sec; else if (Interval.equals("n")) ret = Conversion.CLng(VBVariant.valueOf(sec / 60)); else if (Interval.equals("h")) ret = Conversion.CLng(VBVariant.valueOf(sec / 3600)); else if (Interval.equals("d") || Interval.equals("y")) ret = Conversion.CLng(VBVariant.valueOf(sec / (24 * 3600))); else if (Interval.equals("w") || Interval.equals("ww")) ret = y * 52 + getWeekShort(d1); // quick hack! not correct for multiple-year diffs else if (Interval.equals("m")) ret = 12 * y + d1.getMonth(); else if (Interval.equals("q")) ret = 4 * y + d1.getMonth() / 4; else if (Interval.equals("yyyy")) ret = y; else { System.out.println("DateVal: unknown interval '" + Interval + "'"); ret = 0; } return ret; } public static Date Now() { return Calendar.getInstance().getTime(); } public static Date DateSerial(int year, int month, int day) { // beim java.util.GregorianCalendar ist der Monat nullbasiert, d.h. Januar Date DateSerial = new GregorianCalendar(year, month - 1, day).getTime(); return DateSerial; } private static int getWeekLong(Date d) { // Return year of the week int week = getWeekShort(d); Calendar cal = Calendar.getInstance(); int month = 0; int year = 0; cal.setTime(d); month = cal.get(Calendar.MONTH); year = cal.get(Calendar.YEAR); if ( (week == 1) && (month == Calendar.DECEMBER) ) year += 1; else if ( (week >= 52) && (month == Calendar.JANUARY) ) year -= 1; return 100 * (year % 10) + week; } private static int getWeekShort(Date d) { Calendar cal = Calendar.getInstance(); cal.setTime(d); return cal.get(Calendar.WEEK_OF_YEAR); } public static void DateDiffTest() { ddt("s", "01.07.2009 10:00:00", "01.07.2009 10:01:00", 60); ddt("n", "01.07.2009 10:00:00", "01.07.2009 10:01:00", 1); ddt("h", "01.07.2009 10:00:00", "01.07.2009 11:00:00", 1); ddt("d", "01.07.2009 10:00:00", "02.07.2009 10:01:00", 1); ddt("d", "01.07.2009 10:00:00", "12.07.2009 10:01:00", 11); ddt("y", "01.07.2009 10:00:00", "02.07.2009 10:01:00", 1); ddt("w", "01.07.2009 10:00:00", "09.07.2009 10:01:00", 1); ddt("ww", "01.07.2009 10:00:00", "09.07.2009 10:01:00", 1); ddt("m", "01.07.2009 10:00:00", "01.08.2009 10:01:00", 1); ddt("q", "01.07.2009 10:00:00", "01.10.2009 10:01:00", 1); ddt("yyyy", "01.07.2009 10:00:00", "01.07.2010 10:01:00", 1); ddt("yyyy", "01.07.2009 10:00:00", "01.07.2011 10:01:00", 2); } private static void ddt(String Interval, String ds1, String ds2, long ret) { try { DateFormat df = (DateFormat)new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); Date d1 = df.parse(ds1); Date d2 = df.parse(ds2); long d = DateDiff(Interval, VBVariant.valueOf(d1), VBVariant.valueOf(d2)); if (d != ret) { System.out.println("d1: " + d1); System.out.println("d2: " + d2); System.out.println(Interval + ": "+ ds1 + " - " + ds2 + " = " + d + " <> " + ret); } } catch (java.text.ParseException e) { System.out.println(e); } } public static void main(String[] args) { DateDiffTest(); } } |
This post has been edited 5 times, last edit by "theuserbl" (Jan 14th 2011, 9:29pm)
Intermediate
Date of registration: Jan 1st 2009
Location: Hanover, Germany
Occupation: Software Engineer
Hobbies: Hilbert Curves
We could remove all public functions and just let DateDiff survive.
Intermediate
Date of registration: Jan 1st 2009
Location: Hanover, Germany
Occupation: Software Engineer
Hobbies: Hilbert Curves
Source code |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static java.util.Date CDate(VBVariant Expression) { if (Expression.isDate()) return (Expression.toDate()); return CDate(Expression.toString()); } public static java.util.Date CDate(String Expression) { java.util.Date ret = null; for (int i = 0; i < CheckDatePatterns.length; i++) { for (int x = 0; x < CheckDatePatterns[i].length; x++) { ret = CreateDateFormater(Expression, Join(CheckDatePatterns[i], CheckDatePatterns[i].length - x)); if (ret != null) { return ret; } } } return new java.util.Date(); } public static VBVariant CDate(Object obj) { return new VBVariant(obj); } |
Beginner
Date of registration: Jan 12th 2011
Location: Brazil
Occupation: Visual Basic Programmer
Hobbies: Drive Muscle Cars
Beginner
Date of registration: Jan 12th 2011
Location: Brazil
Occupation: Visual Basic Programmer
Hobbies: Drive Muscle Cars
Beginner
Date of registration: Jan 12th 2011
Location: Brazil
Occupation: Visual Basic Programmer
Hobbies: Drive Muscle Cars
Intermediate
Date of registration: Jan 1st 2009
Location: Hanover, Germany
Occupation: Software Engineer
Hobbies: Hilbert Curves
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 |
Public Sub Command1_Click() Dim now As Date now = New Date() Debug.Print now Debug.Print Year(now) & "-" & Month(now) & "-" & Day(now) Debug.Print Year(now) & "-" & Right("0" & Month(now), 2) & "-" & Right("0" & Day(now), 2) End Sub |
In this case is Date not a Jabaco-function. Date comes from Java and can be found at java.util.Date.Please clarify in the code above: As a VB programmer, I consider a "Date" as a data type, not an object, therefore the "new" keyword would never be used. Dates are dim'd just like integers, etc...
In Java it is an object. And because Jabaco have currently not an own one, it usess the Java one. Right.Quoted
The above code includes "now = New Date()" which implies that the "Date" item in Jabaco is not a data type, but an object. Is this true?
Yes. It compiles to the same binary and so it runs the same.Quoted
Next question: would "now = New Date" execute the same as "now = New Date()" as above?
Have now tested with Set MyVar = New String and MyVar = New String . Both producing the same binary.Quoted
Last question! In VB these statements would fail at runtime, since whenever you are assigning an object to a previously dimmed (but empty) object variable, you need to use "set" as in "Set MyVar = New SomeObject". In Jabaco, is "set" unnecessary, or ignored like good old "let" (as in "let a=5" from the good old days...)
Jabaco Source |
|
1 2 3 4 |
Public Sub main(ByJava args() As String) Dim now As Date now = New Date() End Sub |
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Date; public class Module1 { public static void main(String args[]) { Throwable Err = null; Date date = new Date(); date.setTime(0); Date now = date; now = new Date(); } public Module1() { } } |