change the display of a JXDATEPICKER - java

I used a JXDATEPICKER in a JFrame. But, the date appears in the following form:
ven.15/10/2014
I want the date appears in the following format: yyyy-mm-dd
thanks.

You can use SimpleDateFormat like this:
SimpleDateFormat sf= new SimpleDateFormat( "yyyy-mm-dd" );
res = sf.format(myDate);

You can use the SimpleDateFormat to format the date in any desired format. Get the standard Date object from JXDatePicker.getDate() method, then use SimpleDateFormat to get the date in yyyy-mm-dd format.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
Date dateFrom = JXDatePicker.getDate();
String resultDateFrom = formatter.format(dateFrom);

Related

Change date format in java from ddMMyyyy to dd-MM-yyyy

I have a jformattedtextfield when i put date in this format "ddMMyyyy".
I want when lost focus jformattedtextfield, it becames and shows "dd-mm-yyyy". How i can do this???
See the below code:
SimpleDateFormat format1 = new SimpleDateFormat("ddMMyyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yy");
Date date = format1.parse("05011999");
System.out.println(format2.format(date));

How to get the correct format of dateand time in mysql?

When I get the date and time in MySQL it retrieves it in this format:
2016-01-14 14:24:00.0
Where does the .0 come from and how do I get this format with Java:
2016-01-14 14:24:00
You can use 2 ways to do this.
Split the date string at '.'
String date = "2016-01-14 14:24:00.0";
String newDate = date.split("\\.")[0];
System.out.println(newDate);
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf.parse("2016-01-14 14:24:00.0");
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
If you are getting date as string, you can use Format to get the format you need:
SELECT DATE_FORMAT(YourDateField, '%d-%m-%Y %T')
FROM YourTable
Have a look here for all possible formats
If you are getting your date as a java.sql.Timestamp, you can get the corresponding java.util.Date instance very easily and then format it to the desired string representation with a java.text.SimpleDateFormat class.

Generic way to convert a given String date in any format to java.sql.Timestamp

I want to convert a string representation of date in any format to java.sql.Timestamp,(format is also provided)
we have tried using Timestamp.valueOf() and Date.valueOf() but both these method requires String in a particular format,What i need is a generic method which will convert a given String date in any format to java.sql.Timestamp
Thanks in Advance
If you have the string and know the format you can try something like:
dateStr = "25 December 2015"
date = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH).parse(dateStr);
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(date.getTimestamp());
Or similar:
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
java.util.Date date = simpleDate.parse("2015-12-31 09:45:52.189");
java.sql.Timestamp = new java.sql.Timestamp(date.getTime());

Convert long timestamp to Java.util.Date in french

I want to know how I can convert a timestamp in the date format.
For example : 1415337782000 should be converted to "7 nov. 2014".
This is what I've tried so far :
Date date=new Date(location.date);
SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
String dateText = df.format(date);
The timestamp is stored in location.date in long type.
Thanks in advance for your help.
You can pass a format to the SimpleDateFormat constructor, like this
SimpleDateFormat format= new SimpleDateFormat("d MMM, yyyy");
String dateText = format.format(new Date());
You can find the constants in this link:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
For better internationalization, you can define a string resource containing the desired format based on the location of the user. Something like
SimpleDateFormat format = new SimpleDateFormat(getResources().getString(R.string.YOUR_FORMAT));

convert a date object to Oracle timestamp type string

I want to convert a date object, ex: new Date(), to a string which has a format like Oracle's time stamp type, ex: 21-OCT-13 11.08.13.858000000 AM. I know I could just get each piece of information in the date object like day, month, year, hour, minute, ... to form the Oracle format string but I really want to know is there a utility to do that instead?
Using SimpleDateFormat#format() you would print a Date as
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS a");
System.out.println(sdf.format(new Date()).toUpperCase());
Output :
21-OCT-13 10.01.38.000000614 AM
See JavaDocs for Date and Time patterns.
Try taking a look at SimpleDateFormats - That would be your best bet and easiest way of doing it.
Eg:
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); //Hours:Minutes:Seconds
String strDate = dateFormat.format(date);
Use SimpleDateFormat.
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("your_format_here"); // dd/MM/yy h:mm:ss a
String formattedDate = sdf.format(date);
System.out.println(formattedDate);

Categories

Resources