Does anyone know how extract my date string & change the format? - java

I have a valid date in my String like this:
String strDate = "Available on 03292013";
I want to extract the date from the strDate String & change it to Available on 03/05/2015
Does anyone know how can I achieve this?

You can achieve this by doing the following steps:
First, use the regex "[^0-9]" to extract the date from your String.
Next, use the SimpleDateFormat to change the format of the extracted date from 'MMddyyyy' to
'MM/dd/yyyy'
Finally, you have to append the formatted date String value to the String “Available on”.
Please find below code for better clarity on the implementation.
package com.stackoverflow.works;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* #author sarath_sivan
*/
public class DateFormatHelper {
private static final String DD_MM_YYYY = "MMddyyyy";
private static final String DD_SLASH_MM_SLASH_YYYY = "MM/dd/yyyy";
public static void main(String[] args) {
DateFormatHelper dateFormatHelper = new DateFormatHelper();
dateFormatHelper.run();
}
public void run() {
String strDate = "Available on 03292013";
System.out.println("Input Date: " + strDate);
strDate = DateFormatHelper.getDate(strDate);
strDate = "Available on " + DateFormatHelper.formatDate(strDate);
System.out.println("Formatted Date: " + strDate);
}
public static String formatDate(String strDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DD_MM_YYYY);
Date date;
try {
date = simpleDateFormat.parse(strDate);
simpleDateFormat = new SimpleDateFormat(DD_SLASH_MM_SLASH_YYYY);
strDate = simpleDateFormat.format(date);
} catch (ParseException parseException) {
parseException.printStackTrace();
}
return strDate;
}
public static String getDate(String strDate) {
return strDate.replaceAll("[^0-9]", "");
}
}
Output:
Input Date: Available on 03292013
Formatted Date: Available on 03/29/2013
Hope this helps...

Try this simple and elegant approach.
DateFormat dateParser = new SimpleDateFormat("'Available on 'MMddyyyy");
DateFormat dateFormatter = new SimpleDateFormat("'Available on 'dd/MM/yyyy");
String strDate = "Available on 03292013";
Date date = dateParser.parse(strDate);
System.out.println(dateFormatter.format(date));

This should do what you want. Note that I'm just manipulating the String without any regards for what it actually contains (a date in this case).
String strDate = "Available on 03292013";
String newStr = strDate.substring(0, 15) + "/"
+ strDate.substring(15, 17) + "/" + strDate.substring(17);
System.out.println(newStr);
Result:
Available on 03/29/2013

Related

Remove the hour,minute and seconds from date gotten from server as JSON

I am getting some JSON data from server that includes dates too. But it shows the date like this 2017-07-20 00:00:00 but I want to just see the date like this:2017-07-20, and i checked the previous questions about this issue but all of them were based on the date in the android side. And the problem is that I get the date as JSON and because of that I don't know how to remove Time from it.
Did you try to simple parse this string like this?
String date_string = "2017-07-20 00:00:00";
String[] parsed = date_string.split(" ");
String your_wanted_string = parsed[0];
System.out.println(your_wanted_string);
EDIT
You have to convert string into Date like here : https://stackoverflow.com/a/4216767/1979882
Convert Date to milliseconds. Or use Calendar class.
Calculate the difference between the values.
An example:
http://www.mkyong.com/java/how-do-get-time-in-milliseconds-in-java/
public class TimeMilisecond {
public static void main(String[] argv) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "22-01-2015 10:20:56";
Date date = sdf.parse(dateInString);
System.out.println(dateInString);
System.out.println("Date - Time in milliseconds : " + date.getTime());
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("Calender - Time in milliseconds : " + calendar.getTimeInMillis());
}
}
String date_from_json="your date goes here";
parseDate(date_from_json);
public String parseDate(String s) {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = null;
String str = null;
try {
date = inputFormat.parse(s);
str = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
You can use my javascript function to do this task from client side:
function formatDate(dateString) {
var date = new Date("2017-07-20 00:00:00"),
dd = date.getDate(),
mm = date.getMonth() + 1,
yyyy = date.getFullYear();
mm = mm < 10 ? '0' + mm : mm;
return dd + '-' + mm +'-' + yyyy;
}
call:
var dateStr = formatDate("2017-07-20 00:00:00");
demo

Encountering issue in SimpleDateFormat parse method in Java

I am trying to use SimpleDateFormat.parse method to parse a date string to Date object, but it is omitting "T" in the final date that is returned. I am passing this date string 2015-04-15T12:55:07.365 and I am getting 2015-04-15 12:55:07.365 in the output. However, the desired output is 2015-04-15T12:55:07.365.
Why is "T" in the final output omitted by this line parsedDate = sdf.parse(transDate);
public static void main(String[] args)
{
try
{
final String pattern = "yyyy-MM-dd'T'hh:mm:ss.SSS"; // example 2015-04-15T12:55:07.365
final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String transDate = "2015-04-15T12:55:07.365";
Date parsedDate = sdf.parse(transDate);
System.out.println("transDate:"+transDate+", parsedDate: "+parsedDate);
}
You never get your desired output 2015-04-15T12:55:07.365
Why?
Because you are printing Date object parsedDate.Date class has it's own toString() method implementation.When you are printing the date object, it means it basically prints the toString() method implementation format.
see the Java doc for details
System.out.println(parsedDate) would give you Wed Apr 15 00:55:07 GMT 2015 which is the toString() representation of the date object.
You can use SimpleDateFormat to parse AND format dates:
SimpleDateFormat sdfParser = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
Date date = sdfParser.parse("2015-04-15T12:55:07.365");
SimpleDateFormat sdfFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
String formattedDate = sdfFormatter.format(date);
System.out.println(formattedDate);
// 2015-04-15T12:55:07.365
You will get desired output here.
public static void main(String args[]) {
{
try {
String transDate = "2015-04-15T12:55:07.365";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
Date date = sdf.parse(transDate);
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date d = sdf.parse(sdf.format(date));
String formattedTime = output.format(d);
System.out.println("transDate:" + transDate + ", parsedDate: " + formattedTime);
} catch (Exception e) {
}
}
}

how to convert string into date? JAVA

I have this string: 7 -Jun- 2014.
I want to convert to java.utils.Date;
I use this Code
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
but I get this exception :
java.text.ParseException: Unparseable date: "7-Jun-2013"
at java.text.DateFormat.parse(Unknown Source)
at ma.abcsolution.util.Test.main(Test.java:15)
try using
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
the SimpleDateFormat look at the given string like it you tell it to, so in your example it look for a string with 2 chars for days, followed by a '/' sign and then 2 chars for month and so on
Using SimpleDateFormatter you can convert from date string to date and date to date String
public final class DateUtil {
private static final String DEFAULT_DATE_FORMAT = "dd-MMM-yyyy";
private DateUtil() {
}
public static final String formatDate(final Date date, final String dateFormat) {
DateFormat format = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
return format.format(date);
}
public static final Date formatDate(final String date) throws ParseException {
return formatDate(date, DEFAULT_DATE_FORMAT);
}
public static final Date formatDate(final String date, final String dateFormat) throws ParseException {
DateFormat format = new SimpleDateFormat(dateFormat);
return format.parse(date);
}
}
Whenever you are going yo format date please verify the format you are using
In your case you used dd/MM/yyyy but date you used in format dd-MMM-yyyy.
Use
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
to format the date. You used the wrong format with the / signs.
See also:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Date date = new SimpleDateFormat("d-MMM-yyyy").parse("7-Jun-2014");
Use this code. It is simple.
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTime {
public static void main(String args[]){
SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "7-Jun-2013";
Date date=new Date(dateInString);
System.out.println(ft.format(date));
}
}

How to format your SimpleDateFormat output

Below, I've got a chunk of code that returns a date in the format of "Sat May 02 00:00:00 MST 1970" (assuming it receives an input of 05/02). How do I format this so that I've just got "May 02"? It doesn't matter to me if I format it here in this block of code or in my print statement.
My current print statement is System.out.print(Date.getAlphabetDate().
public static Date getAlphabetDate()
{
try
{
String tempDate = month + "/" + day;
Date alphabetDate = new SimpleDateFormat("M/d").parse(tempDate);
return alphabetDate;
}
catch(Exception e)
{
return null;
}
}
You are misunderstanding what SDF does. It returns a Date object, not the String representation of that object. And while a Date object has all that baggage that you're not interested in, you shouldn't care too much about it, since to get a similar String representation of the Date returned, simply use the same or a similar SDF object and use it to format the date:
String stringRep = mySDF.format(myDate);
As an aside, note that whenever code is written with a catch block that returns null, a puppy dies somewhere.
Edit
For example,
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SdfFun {
private static final String PATTERN = "MM/dd";
private static final SimpleDateFormat SDF = new SimpleDateFormat(PATTERN);
public static Date getAlphaDate(int month, int day) throws ParseException {
String tempDate = month + "/" + day;
return SDF.parse(tempDate);
}
public static void main(String[] args) {
Date date = null;
try {
date = getAlphaDate(2, 16);
System.out.println(SDF.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}

How to convert string to date format

I want to convert 3/13/2014 11:38:58 AM string to date format.
I see some examples but and also implement but I don't know how to convert AM/PM to 24 hour time format.
How to make it possible ?
Use SimpleDateFormat
Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").parse(string);
Using this you can convert your date and time..
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("3/13/2014 11:38:58 AM");
System.out.println("now time is.." + date_start);
Thanks..
Parsing Strings into Dates:
The SimpleDateFormat class has some additional methods, notably parse( ) , which tries to parse a string according to the format stored in the given SimpleDateFormat object. For example:
import java.util.*;
import java.text.*;
public class DateDemo {
public static void main(String args[]) {
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
String input = args.length == 0 ? "1818-11-11" : args[0];
System.out.print(input + " Parses as ");
Date t;
try {
t = ft.parse(input);
System.out.println(t);
} catch (ParseException e) {
System.out.println("Unparseable using " + ft);
}
}
}

Categories