Dear visitor, welcome to Jabaco - Community.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
Convert date to mysql format
The format for mysql date is yyyy-mm-dd but that for windows is different.When i make an sql statement in which i will input date.It gives error.I need help.
A simple fix I can think of is to simply input the date as a string, then grab the date via Jabaco (I don't know what function does it in Jabaco as I've been forgetting my Basic Syntax lately) but it should stop MySQL from crying and make it possible for people from other countries who output their date / time information to be able to not have a mest up MySQL (I know this because my friend used this solution to the same problem
) The MySQL wouldn't let me register, so he set date and times to a string instead.
How can I do this in jabaco
Quite often there is a need to get current time in Java in a specific format. I faced this issue when I had to insert the current time into MySQL. I solved it using a SimpleDateFormat class and a helper function. Here is the code.
The java.text.SimpleDateFormat class is very useful in formatting the date in Java. In my case, I had to convert the date to a format suitable for MySQL insert. So I used this format: String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
Then I created a function that uses this format and converts the current date and returns a string with the current date in this format. Here is the complete code.
1. Step 1: import the required classes
import java.util.Calendar;
import java.text.SimpleDateFormat;
2. Step 2: Initialize the variables
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
3. Step 3: create the function to get current date and convert to the above format
public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
4. Step 4: Use the now function to get formatted date in MySQL format.
System.out.println("Now : " + now());
It is as easy as that. With this concept you can use it with different formats.
'now' without external Java class
Hi,
without compiling Java classes, you can do it in pure Jabaco as follows:
|
Source code
|
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
|
Public Function myNow As String
Dim cal As java#util#Calendar
Dim sdf As java#text#SimpleDateFormat
Const DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"
cal = java#util#Calendar.getInstance
sdf = New SimpleDateFormat(DATE_FORMAT_NOW)
myNow = sdf.format(cal.getTime())
End Function
Public Function myNow2 As String
Dim sdf As java#text#SimpleDateFormat
sdf = New SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
myNow2 = sdf.format(Calendar.getInstance.getTime())
End Function
Public Function myNow3 As String
' more a puzzle for novice programmers than a decent implementation ...
Dim cal As java#util#Calendar = Calendar.getInstance
Dim i As Integer
Dim c, s As String = cal.get(1) & "-"
For i = 0 To 16 Step 4
c = mid("021-050 110:120:130 ", i+1, 4)
s = s & right("0" & (cal.get(left(c, 2)) + cint(mid(c, 3, 1))),2) & right(c, 1)
Next i
myNow3 = s
End Function
|
Cheers!
A1880
This post has been edited 1 times, last edit by "A1880" (Apr 23rd 2009, 10:35pm)