PHP timestamp to Java timestamp - java

I need to convert this 1384174174 timestamp from PHP to Java. This is how I echo the date('Y/m/d H:i:s' ,$dn1['timestamp'] in PHP yet I don't know how to do it in Java. Please help me. Thanks.

In Java, you'd do it like this:
Date date = new Date(1384174174);
SimpleDateFormat f = new SimpleDateFormat("Y/M/d H:m:s");
String dateFormatted = f.format(date);
Watch out for the format pattern where, unlike PHP, M is month in year and m is minute in hour.

This method will take your Unix style date and create a Java Date. It returns a readable string. It should help you get started.
private String unixToString(long unixSeconds) {
Date date = new Date(unixSeconds);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}

Related

DateTime from PHP to Java (Android): how to convert a string to a DateTime object?

In PHP to convert a string to DateTime() its very very easy:
$dateTime = new DateTime("2013-12-11 10:109:08");
echo $dateTime->format("d/m/Y"); // output 11/12/2013
What is the equivalent in Java? I've seen a lot of questions in stackoverflow. I cant find a way to solve this problem.
My last try is:
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ITALIAN);
return dateFormat.format(new Date(datetime)).toString();
This crash application. Android Studio tells me that Date(java.lang.String) is deprecated.
Can someone help me?
// First convert the String to a Date
String dateTime = "2013-11-12 13:14:15";
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ITALIAN);
Date date = dateParser.parse(dateTime);
// Then convert the Date to a String, formatted as you dd/MM/yyyy
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormatter.format(date));
You can let the parser / formatter take the timezone into account by using SimpleDateFromat.setTimeZone() if you have to deal with TimeZones that are not in your default locale.
try this
String time1="";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS",Locale.US);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
calendar.setTimeInMillis(yourmilliseconds);
time1=sdf.format(calendar.getTime());
Yes As of JDK version 1.1, Date(java.lang.String) is deprecated and replaced by DateFormat.parse(String s).
Parse it like that:
SimpleDateFormat formatter =
new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY);
Calendar date = Calendar.getInstance();
try {
date.setTime(formatter.parse("12.12.2010"));
} catch (ParseException e) {
e.printStackTrace();
}
Have a look at my Android date picker example here.

Simple java date conversion

Having some troubles and can't find a quick answer..
Im trying to store a date within a string, and later fetch it to convert it back to a date.
However when storing the date using:
string tmp = new Date().toString();
And then trying to convert it back using
Date date = new Date(tmp);
I get the Exception type
java.lang.IllegalArgumentException
with my Android 2.2 device. Does work with 2.2 & 2.3 emus tho.
Any tips on what other way i can store and convert back?
You could use SimpleDateFormat with its methods parse() and format().
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
String tmp = sdf.format(new Date());
Date date = sdf.parse(tmp);
Do you need it to be a string? long is easier :)
do
long time = new Date().getTime();
Date date = new Date(time);
then you dont' have to parse
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// to string
String dateStr = formatter.format(new Date());
// to date
Date date = formatter.parse(dateStr);
use SimpleDateFormat as shown below.
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
//this will convert date into string.
String temp = formatter.format(currentDate.getTime());
//this will convert string into date format.
Date date=(Date)formatter.parse(temp);

Java format milliseconds to uu:mm:ss

I have the following problem and I couldn't find the best solution for it yet.
Lets say I have an integer with the following value:
int miliseconds = 65111;
I want to print it to an stream using the printf() function. Is there a way I can do the following:
printf("Time: %uu:mm:ssT", miliseconds");
so it will return:
Time: 00:01:05
Here I just made up the %uu:mm:ssT part, but is there a way to do this.
Also, do you know a website where I can find all the formatting options, so I can look it up myself next time.
Use
final int miliseconds = 65111;
System.out.printf("%1$TM:%1$TS.%1$TL\n", (long) miliseconds);
and see Format String Syntax for details.
Have a look at the SimpleDateFormat feature.
Consider the following sample
String inputPattern = ....
String outputPattern = ....
String ms = ((Integer) milliseconds).toString();
DateFormat inFormat = new SimpleDateFormat(inputPattern);
DateFormat outFormat = new SimpleDateFormat(outputPattern);
// Parse input
Date date = inFormat.parse(ms);
// Format the output
String output = outFormat.format(date);
[Edit] - Updated sample to only use j2se instead of including yodatime.
If you use the SimpleDateFormat to format a time from new Date(milliseconds), don't forget that SimpleDateFormat is time zone sensitive. So set it to UTC before using, like this:
DateFormat outFormat = new SimpleDateFormat("HH:mm:ss");
outFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = new Date(milliseconds);
String result = outFormat.format(d);
(I'm using a similar code in my program for a JSpinner to input a millisecond time value.)

Date conversion in Java

How can I take a string in a format such as: 2008-06-02 00:00:00.0 and convert it to: 02-Jun-2008?
Can I somehow take the original string, convert it to a Date object, then use a formatter to get the final output (rather than parsing the string myself)? Thanks!
You can use SimpleDateFormat to convert between a String and a Date object and vice versa based on a pattern. Click the API link, you'll see patterns being explained in detail. A 4-digit year can be represented with yyyy, a 3-character month abbreviation can be represented with MMM and so on.
First you need to parse the String of the first format into a Date object:
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf1.parse(inputString);
Then you need to format the Date into a String of the second format:
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
String outputString = sdf2.format(date);
Note that you need to take the Locale into account as well to get the month to be printed in English, else it will use the platform's default locale to translate the month.
Use 2 instances of SimpleDateFormat class. One for converting your input string to date and second to convert date back to string but in another format.
Here is an example of using SimpleDateFormat.
DateFormat startFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat endFormat = new SimpleDateFormat("dd-MM-yyyy");
String outputString = null;
try {
Date date = startFormat.parse(inputString);
outputString = endFormat.format(date);
} catch(ParseException pe) {
throw new IllegalArgumentException(inputString + " is not properly formated.", pe);
}
You can definitely use SimpleDateFormat class like others have recommended.
Another suggestion if it applies in your case. If you are getting this data from a sql query you can also use to_char() method to format it in the query itself. For example: to_char(column_name,'DD-MON-YYYY')

How to convert String object to Date object? [duplicate]

This question already has answers here:
How to convert ISO 8601 date (string) to Date?
(3 answers)
Closed 5 years ago.
How can I convert a String object to a Date object?
I think I need to do something like this:
Date d=(some conversion ) "String "
Any help would be greatly appreciated.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date date = dateFormat.parse("1.1.2001");
For details refer to: SimpleDateFormat documentation
Date-to-String conversion is a relatively complex parsing operation, not something you can do with a simple cast as you are trying.
You'll have to use a DateFormat. It can be as simple as:
Date d = DateFormat.getDateInstance().parse("09/10/2009");
But this changes the expected date format depending on the locale settings of the machine it's running on. If you have a specific date format, you can use SimpleDateFormat:
Date d = new SimpleDateFormat("d MMM yyyy HH:mm").parse("4 Jul 2001 12:08");
Note that the parse method will always expect one specific format, and will not try to guess what could be meant if it receives a different format.
See Sun's Java tutorial and the class SimpleDateFormat
Use a SimpleDateFormat with a format string, which matches your actual format:
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2009-10-09");
java.text.SimpleDateFormat that extends java.text.DateFormat abstract class.
DateFormat MYDate = new SimpleDateFormat("dd/MM/yyyy");
Date today = MYDate.parse("09/10/2009");
you should parse the string with the SimpleDateFormat class
use
Date date = DateFormat.getInstance.parse( dateString );
You can convert String object into Date object using this method. and this Java code is tested and running component in my environment.
public static Date parseStringAsDate(String dateStr, String format) throws ParseException
{
if(null==dateStr || "".equals(dateStr))
throw new IllegalArgumentException("dateStr must not be null or empty");
DateFormat df = new SimpleDateFormat(format);
return df.parse(dateStr);
}
dateStr = "17/05/2017"
format= "dd/MM/yyyy"

Categories