Java SimpleDateFormat use pattern with jolly character - java

Is there a way to parse a date with a pattern containing some "special char" or "jolly char" as separator with standard SimpleDateFormat? I want to parse my date using the patterns "yyyy MM dd", "yyyy/MM/dd", "yyyy-MM-dd" and so on..., so I'm searching something like "yyyy*MM*dd" where * is a special character meanings 'a random character'
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date d = fmt.parse(stringdate);

Use a RegEx to filter out the funny characters, then parse the filtered string?
String stringdate = "2017x01-18";
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date d = fmt.parse(Pattern.compile("(....).(..).(..)").matcher(stringdate).replaceAll("$1-$2-$3"));

you should try this
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);

You can try to 'normalize' your input to desired format:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
fmt.parse(stringdate.replaceAll(" ", "-").replaceAll("/","-"));

Related

How to remove year in DateFormat follower locale?

I use DateFormat to show Day and Month. DateFormat supports MEDIUM, LONG, FULL all have a year. I want to remove year from this code and how can I achieve this
SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, locale);
sdf.applyPattern(sdf.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
But I get an error with some locale like:
bo_CN : སྤྱི་ལོ་y MMMMའི་ཙེས་dད
vi: 'Ngày' dd 'tháng' MM 'năm' y
se_SE: d 'de' MMMM 'de' y
You can use SimpleDateFormat:
Date date = new Date();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd");
String dateString = df.format(date);
Output:
01/21
Try with the below code
String shortDate = "dd.MM";
String mediumDate = "MMM dd";
String longDate = "MMMM dd";
String fullDate = "EEEE, MMMM dd";
Locale locale = new Locale("EN");
DateFormat df = new SimpleDateFormat(fullDate, locale); \\Use the required format here
String formattedDate = df.format(new Date());
I have removed the year part and created custom equivalent of
DateFormat.SHORT as shortDate
DateFormat.MEDIUM as mediumDate
DateFormat.LONG as longDate
DateFormat.FULL as fullDate

How to format a date

I'd like to calculate the time difference between two dates, but I'm unable to parse a string representation of a date that looks like 2015-01-13 15:59:10, for example, through a formatter SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Only string dates in the form, String dateStart = "01/14/2012 09:29:58";, for example work.
How should I go about parsing 2015-01-13 15:59:10, for example through SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");?
Your format needs to match the literal you're trying to parse:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
instead of
format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Your format must match your input string. The correct format is:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
See the documentation for more details and an explanation.
to parse 2015-01-13 15:59:10 you should change the date format to below:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

Java Formatting Date String

I need to convert this 20140815
into 15.08.2014 and into 15.08.2014 00:00:00
How do I do that, what opportunities are there in Java
You could use SimpleDateFormat as follows
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat outputFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
Date date = inputFormat.parse("20140815");
String result = outputFormat.format(date);
Why bother with Dates... just use Strings:
String output = input.replaceAll("(....)(..)(..)", "$3.$2.$1");
or
String output = input.replaceAll("(....)(..)(..)", "$3.$2.$1 00:00:00");

How to parse a Date like this 20-Feb-2006?

i have problem of parsing a String into Date when the month contains 3 letters instead of two
use DateFormat in this way:
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println( df.parse("20-Feb-2006"));
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date date = df.parse("20-Feb-2006");
USe
String strDate = "20-Feb-2006";
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date dateStr = formatter.parse(strDate);

How to convert date and time stored in string to 24 format time in android?

I have my date and time stored in string i.e my string contains str ="18/01/2013 5:00:00 pm". How can I convert it to 24 format time in android?
You can use two SimpleDateFormat instances: one to parse the input as a date and a second to format the date as a string with the desired format.
For example, formattedDate in the code below will be 18/01/2013 17:00:00:
String str = "18/01/2013 5:00:00 pm";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date dt = input.parse(str);
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = output.format(dt); //contains 18/01/2013 17:00:00
Notes:
hh is for Hour in am/pm (1-12) whereas HH is for Hour in day (0-23).
for more formatting options, check the javadoc
try
String str ="18/01/2013 5:00:00 pm";
Date date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").parse(str);
str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
System.out.println(str);
output
18/01/2013 17:00:00
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "18/01/2013 5:00:00 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use SimpleDateFormat for that:
SimpleDateFormat dateFormat = new SimpleDateFormat(dd/mm/yyyy HH:mm:ss");
Refer to this which was opposite to your requirement. Just posted the link so you might get the idea of difference that HH and hh makes.
Try using the java SimpleDateFormat class.
Example:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
df.parse(date);
The upper case HH use a 24h format

Categories