I created a table :
create table Appointment(
App_ID number primary key,
Doctor_ID number,
Patient_ID number,
App_Date Date,
App_Time TIMESTAMP,
App_Charges number);
i know how to convert String to java.sql.Date.
But for time i'm doing
String s1=time.getSelelctedItem().toString();//specifying time from a combo box
and then
st.setString(5,s1);
Please tell me the changes i need to make..
Thanks.
I'd use a java.util.Date in the middle:
// Specifying time from a combo box
String s1 = time.getSelelctedItem().toString();
// Convert String to Date according to the format
SimpleDateFormat df = new SimpleDateFormat("hh:mm");
Date date = df.parse(s1);
// Convert Date to Timestamp
Timestamp ts = new Timestamp(date.getTime());
// Set it:
st.setTimestamp(5, ts);
check the below code :
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "29/03/2014";
try {
Date date = formatter.parse(dateInString);
java.sql.Timestamp timest = new java.sql.Timestamp(date.getTime());
System.out.println(timest.getDate());
} catch (Exception e) {
e.printStackTrace();
}
so timest is the timestamp got from date which is parsed from datestring "29/03/2014" using SimpleDateFormat.
Related
I have date as a string like this
String date = "11-12-2018"
I want to change it to "2018-12-11"
with the same variable. So, I tried the code below but it doesn't give me the output I expect.
String date = "11-12-2018"
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date d = df.parse(date);
results in:
"0012-06-09"
I want
"2018-12-11"
You can do this 3 ways. First is using SimpleDateFormat and Date and second using DateTimeFormatter and LocalDate and third you can use Split.
1. Using Date and SimpleDateFormat
String date = "11-12-2018";
SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date d = df.parse(date);
String finalDate = new SimpleDateFormat("yyyy-MM-dd").format(d);
System.out.println(finalDate);
Here we have our actual date String date = "11-12-2018"; we know we want to change it to 2018-12-11
So lets parse that date into a Date object using this code
SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date d = df.parse(date);
Okay so now we have a date object of our actual date, Now lets format it to our new date.
String finalDate = new SimpleDateFormat("yyyy-MM-dd").format(d);
2. Using LocalDate and DateTimeFormatter
Alright here we define our date again and 2 DateTimeFormatter.
DateTimeFormatter oldFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
The first formatter is our old date format, and the second one is the new one that we are gonna convert the old date into.
Alright lets use them now!
Now we make a new LocalDate object using our oldFormatter by parsing our dateString with the oldFormatter object
LocalDate dateTime = LocalDate.parse(date, oldFormatter);
Alright now lets format it.
String reformattedDate = dateTime.format(newFormatter);
as simple as that! Here is the full code.
String date = "11-12-2018";
DateTimeFormatter oldFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate dateTime = LocalDate.parse(date, oldFormatter);
String reformattedDate = dateTime.format(newFormatter);
System.out.println(reformattedDate);
3. Using String::Split
Okay this part is pretty simple. Lets split the date using -
String[] dates = date.split("-");
We already know the order of the date lets format it using String::format
String reformattedDate = String.format("%s-%s-%s", dates[2], dates[1], dates[0]);
Here is the full code
String date = "11-12-2018";
String[] dates = date.split("-");
String reformattedDate = String.format("%s-%s-%s", dates[2], dates[1], dates[0]);
System.out.println(reformattedDate);
Try code below that will work for your case:
First parse your input format from string,
String date = "11-12-2018";
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Then convert it to desired format,
Date dateTobeParse = null;
try {
dateTobeParse = df.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
if (dateTobeParse != null) {
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
String outputDate = outFormat.format(dateTobeParse);
}
This is the common function which I use for date and time conversion
public String convertDateAndTime(String date, String oldFormat, String newFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
Date currentdate;
String converted = "";
try {
currentdate = sdf.parse(date);
SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);
converted = sdf2.format(currentdate);
} catch (ParseException e) {
e.printStackTrace();
}
return converted;
}
You just have to pass the date string and their old and new formats.
In your case call this function like this
String converteddate = convertDateAndTime("11-12-2018","dd-mm-yyyy","yyyy-MM-dd");
Try the code below that will work
1) Make method like below
public String changeDateFormat(String currentFormat, String requiredFormat, String dateString) {
String result = "";
SimpleDateFormat formatterOld = new SimpleDateFormat(currentFormat, Locale.getDefault());
SimpleDateFormat formatterNew = new SimpleDateFormat(requiredFormat, Locale.getDefault());
Date date = null;
try {
date = formatterOld.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
if (date != null) {
result = formatterNew.format(date);
}
return result;
}//end of changeDateFormat()
1st argument of the method is your current date format in your case it will be 'dd-MM-yyyy'
2nd argument is output or requires date format in your case it will be 'yyyy-MM-dd'
3rd argument is your date that you want to change the format
2) Run the method like below
String oldFormatDate = "11-12-2018";
String myDate = changeDateFormat("dd-MM-yyyy", "yyyy-MM-dd", oldFormatDate);
Log.d(TAG, "Old formatted Date : " + oldFormatDate);
Log.d(TAG, "New Date is : " + myDate);
3) Output:
Old formatted Date : 11-12-2018
New Date is : 2018-12-11
Hi can anyone help please? I am trying to format a date and time string.
Currently it looks like this "20160112T110000Z" and I need it to be "2016-01-12T11:00:00Z"
The string without the special characters are returned from a 3rd party recurrence library. I need to convert it to have the special characters before parsing it to a Calendar object.
Can anyone help please?
The code that I have so far looks like:
final String TIMEFORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
String string = "20160112T110000Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = format.parse(string);
System.out.println(date);
However this just does not work.
Any suggestions are appreciated
You have to read the string with a format matching the source, this gives you a correct Date.
Then simply write it with the format you want :
String string = "20160112T110000Z";
String originalStringFormat = "yyyyMMdd'T'HHmmss'Z'";
String desiredStringFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
SimpleDateFormat readingFormat = new SimpleDateFormat(originalStringFormat);
SimpleDateFormat outputFormat = new SimpleDateFormat(desiredStringFormat);
try {
Date date = readingFormat.parse(string);
System.out.println(outputFormat.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
try this
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// you can change format of date
Date date = formatter.parse(strDate);
Timestamp timeStampDate = new Timestamp(date.getTime());
return timeStampDate;
In my Java Cloud Endpoints API I have some code to get the current date and then store that date in my Cloud SQL (MySQL) database:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
//formattedDate is then inserted into the database
Later on in my Android Activity I query the database and get the date back as a String that looks like:
2015-06-24 17:53:01
Now I want to format this date to display it like 06/24/2015 on the UI of my Activity. To accomplish this I do the following:
//I get a string like 2015-06-24 17:53:01 passed in from another activity
//which in-turn got it from the MySQL database
datetime = getIntent().getExtras().getString("datetime");
//first convert the string datatime to a date object
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
}
//then format that date object the way you want
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = df.format(convertedDate);
//set the TextView in my Activity to display 06/24/2015
myDateTextView.setText(formattedDate);
This works. But man is it a convoluted way to do something simple. I am wondering if there is a more efficient way to do this?
You should save long value represented date into DB.
long l = c.getTimeInMillis()
and this value you should save.
answer on your question below:
Date date=new Date(l);
SimpleDateFormat df2 = new SimpleDateFormat("MM/dd/yyyy");
String dateText = df2.format(date);
System.out.println(dateText);
The simplest way is to store date in long value at DB, after that you can format to string in a very simple way.
DateFormat.format("MMM dd, yyyy", milliseconds).toString();
If you can change the source code of insertion into database, try inserting the timestamp in Long. Then conversion of timestamp to date format should be straight forward.
in Android,
final String data = DateFormat.format("MM/dd/yyyy",timestamp);
I have a form on which I want to access a date from the database and show in jDateChooser for a particular record.
I saved the date as a string in the database.
How do I get the date from the database table and how do I set that date in jDateChooser?
If you stored the date in the database as String then you're going to need to retrieve it as String
String dateValue = resultset.getString(...); // What ever column
java.util.Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateValue);
jDateChooser.setDate(date);
If the date chooser that you've mentioned is this one http://plugins.netbeans.org/plugin/658/jdatechooser-1-2 then one possible solution might be this.
String dateValue = resultset.getString(2); // Your column Name
java.util.Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateValue);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
jDateChooserDCC.setSelectedDate(cal);
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
Date dateValue = null;
try {
dateValue = date.parse(resultset.getstring(1));
} catch (ParseException ex) {
Logger.getLogger(pegawai.class.getName()).log(Level.SEVERE, null, ex);
}
jdateChooser.setDate(dateValue);
note : resultset.getstring(1) is data from database mysql
In mysql, i have a field time_entered of type datetime (sample data: 2012-06-20 16:00:47). I also have a method, getTimeEntered(), that returns the value as String. I want to display the date in this format 2012-06-20 using DateTimeFormat from GWT.
here's my code:
String date = aprHeaderDW.getTimeEntered();
DateTimeFormat fmt = DateTimeFormat.getFormat("MM-dd-yyyy");
dateEntered.setText("" + fmt.format(date));
The problem is, the format method doesn't accept arguments as String. So if there's only a way I could convert the date from String to Date type, it could probably work. I tried typecasting but didn't work.
You should be able to just use DateTimeFormat.
Date date = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
String dateString = DateTimeFormat.getFormat("yyyy-MM-dd").format(date);
Otherwise there is a light-weight version of SimpleDateFormat that supports this pattern.
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
Hi There are two options.
The first is as it is already a string you could use a regular expression to modify the format.
The second is using a SimpleDateFormater you can parse the string to a date then back again.
For example:
public class DateMerge {
public static void main(String arg[])
{
String out = dateConvert("2012-06-20 16:00:47");
System.out.println(out);
}
public static String dateConvert (String inDate)
{
try {
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = (Date)formatter.parse(inDate);
formatter = new SimpleDateFormat("dd-MM-yyyy");
String outDate = formatter.format(date);
return outDate;
} catch (ParseException e)
{System.out.println("Exception :"+e); }
return null;
}
}
You may use like this.
String date = "2012-06-20 16:00:47";
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
String lDate=sf.format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date));
System.out.println(lDate);
Output:
2012-06-20
After trying a lot of times I came up with a solution, based on #Keppil and adding my own code.
Here's Keppil's suggested solution for converting String datetime into Date type:
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
...but my second requirement is to display just the date like 2012-06-20. Even though I removed HH:mm:ss, it still displayed the time like this 2012-06-20 00:00:00.
Here's my final solution:
Date date = null;
String d = rs.getString(SQL_CREATION_TIME); // assigns datetime value from mysql
// parse String datetime to Date
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(d);
System.out.println("time entered: "+ date);
} catch (ParseException e) { e.printStackTrace(); }
// format the Date object then assigns to String
Format formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd");
String s = formatter.format(date);