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

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));

Related

How to format the given date "31.12.9999" in the format "yyyy/MM/dd HH:mm:ss"

i have a common doubt for a long while. During date formation if the input date in the format like "2019/05/22 02:00:23" then with the help following line we can format the date,
String inputDate = "2019/05/22 02:00:23";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
I can see both input format and the required format is same. Suppose if i change the input date like below, it is showing the java.text.ParseException: Unparseable date: exception.
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
Please suggest how to achieve this.
Please try below i mentioned date format code,I hope it will help you,
From this,
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
To Change:
Date date = new SimpleDateFormat("dd.MM.yyyy").parse("31.12.9999");
String formattedDate = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date);
System.out.println("formattedDate :: " + formattedDate);
Thanks,.

change the display of a JXDATEPICKER

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);

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));

Why won't my SimpleDateFormat convert correctly?

I have my Date as String: 2014-06-23 22:00
To get the Date as java.util.date I parse it by using SimpleDateFormat
Date listDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm",Locale.GERMANY);
listDate = sdf.parse(gameList.get(position).getTime());
but my output
System.out.println(listDate)
is 2014-04-118 19:37
What's going on here??
DD should be dd (in small) below are the available formats in Java7

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