Java How to read date dynamically - java

I have a string with six numbers: 650310. It represents 1965 march 10 in YYMMDD format.
Is there any method to recognize this format to 10 march 1965?
Currently this is my method of doing which isn't very effective.
public class Example {
public static void main(String args[]) {
//date in YYMMDD
//String x = "650310";
String x = "161020";
System.out.print(x.substring(4, 6)+" ");
if (Integer.parseInt(x.substring(2, 4)) == 10) {
System.out.print("October"+" ");
}
else if (Integer.parseInt(x.substring(2, 4)) == 03) {
System.out.print("March"+" ");
}
if (Integer.parseInt(x.substring(0, 2)) > 50) {
String yr = "19" + x.substring(0, 2);
System.out.println(yr);
} else if (Integer.parseInt(x.substring(0, 2)) < 50) {
String yr = "20" + x.substring(0, 2);
System.out.println(yr);
}
}
}
output : 20 October 2016

Use Java's SimpleDateFormat:
SimpleDateFormat inFormat = new SimpleDateFormat( "yyMMdd" );
Date theDate = format.parse( "650310" );
Now you have a Date object which you can use to display the date in other formats:
SimpleDateFormat outFormat = new SimpleDateFormat( "dd MMMMM yyyy" );
StringBuffer output = outFormat.format( theDate );
Use output.toString() to display your newly formatted date. Good luck.

try this example
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Example {
public static void main(String args[]) throws ParseException {
SimpleDateFormat s = new SimpleDateFormat( "yyMMdd" );
Date theDate = s.parse( "650310" );
SimpleDateFormat p = new SimpleDateFormat( "dd MMMMM yyyy" );
System.out.println(p.format(theDate));
}
}
OUTPUT 10 March 1965

This link will help. Create a SimpleDateFormat object and use it to parse Strings to Date and to format Dates to Strings.

Use SimpleDateFormat for date parsing. For example:
SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
try {
System.out.println(format.parse("900310"));
} catch (ParseException e) {
e.printStackTrace();
}
Output: Sat Mar 10 00:00:00 MSK 1990
EDIT: if you want to parse date,try to use DateFormat to get Date !!!! And then you can format it in your own way. I disagree with your downvote.
SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
try {
Date parse = format.parse("900310");
format.applyPattern("dd MMMM yyyy");
System.out.println(format.format(parse));
} catch (ParseException e) {
e.printStackTrace();
}
output 10/Март/1990

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

Convert time to 24 hours format

I have some time value which is in String format (for example 12:45 AM and 7:00 PM).
I wonder how should I convert it into the 24 hours format (for example 12:45 and 19:00).
Should the output be in long format?
Please take a look at this page.
It has examples how to convert it back and forth.
Here is the 12hrs to 24hrs conversion.
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
import java.text.ParseException;
String input = "2014-12-20 10:22:12 PM";
//Format of the date defined in the input String
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");
//Desired format: 24 hour format: Change the pattern as per the need
DateFormat outputformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
String output = null;
try{
//Converting the input String to Date
date= df.parse(input);
//Changing the format of date and storing it in String
output = outputformat.format(date);
//Displaying the date
System.out.println(output);
} catch (ParseException pe) {
pe.printStackTrace();
}
String dateStr = "12:45 AM";
DateFormat inputFormat = new SimpleDateFormat( "hh:mm aa" );
DateFormat outputFormat = new SimpleDateFormat( "HH:mm" );
Date date = null;
try{
date = inputFormat.parse( dateStr );
}
catch ( ParseException e ){
e.printStackTrace();
}
if( date != null ){
String formattedDate = outputFormat.format( date );
}
I'm not sure whether I get your question right, but shouldn't the following work?
Date date=new Date("01/01/14 " + myTimeString); // Example: 8:22:09 PM
System.out.println("Formattet ="+new SimpleDateFormat("HH:mm").format(date));

converting string to date and converting date back to string

Hi for some of the requirement i need to convert the string representation of date(with no format) to date object and convert back to string(with a specific format)
This is what i tried so far, the output is not coming as expected and it's printing something like 08140009 - Any idea what is this
And please provide any suggestions.
MY code is :
public String getDateBackToCST(String createDate){
SimpleDateFormat dateFormatter = new SimpleDateFormat("MMddyyyy");
TimeZone obj = TimeZone.getTimeZone("CST");
dateFormatter.setTimeZone(obj);
Date createdDate = null;
try {
createdDate = dateFormatter.parse(createDate);
} catch (ParseException e) {
e.printStackTrace();
}
return dateFormatter.format(createdDate);
}
You need to specific proper flags for SimpleDateFormat. You have 2 options to specify timezone z and Z and to specify day name use E like this
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) throws ParseException {
String date = "Sat Sep 20 23:39:04 IST 2014 ";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
System.out.println(sdf.parse(date));
}
}

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

convert string to date and format the date

I have String variable which contains date. Now how to display July 15 from that string variable? (for example 2012-07-15)
String strdate = "2012-07-15";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd");
try {
d1 = formatter.parse(strdate);
System.out.println(d1 + "---date first---");
} catch (ParseException e) {
e.printStackTrace();
}
Your parsing template does not match your example:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
(with MM instead of MMM).
To output July 15, you can use a second formatter:
SimpleDateFormat output = new SimpleDateFormat("MMMM dd");
System.out.println(output.format(d1)); //July 15
Something like,
try {
String strdate = "2012-07-15";
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd"");
DateFormat df2 = new SimpleDateFormat("MMMM dd");
System.out.println(df2.format(df1.parse(strdate)+ "---date first---");
}
catch (ParseException e) {
// Exception handling
}

Categories