How to delete some character from respone in Java? - java

I have some string respone date like this:
2018-11-30 12:00:00
and i want just get the time, so i want like this:
12.00
and my question, can we delete some character like that?

Sounds like you want to use SimpleDateFormat so something like:
Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String folderName = formatter.format(today);
Based on your comment try:
int index = time.indexOf(‘:’);
int start = index -2;
int end = index + 2;
String newTime = time.substring(start,end);

String apiDateString = "2018-11-30 12:00:00"; // in this case, your string from api
Date apiDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(apiDateString); // convert api date string to date
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String yourDateString = formatter.format(apiDate);
I think you want this.
Edited with little comment.
convert string to date. (apiDateString to apiDate)
create new format for what you want to form. In this case, 12.00
convert apiDate to your format. (apiDate -> 12.00)

You can try this
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
String strDate = "Current Time : " + mdformat.format(calendar.getTime());
display(strDate);

You can use SimpleDateFormat to get time and then replace : by .
I hope it can help your problem!

You can refer to this post.
Use SimpleDateFormat to format you date.
From String to Date
// Convert from string to date
String yourDate = "2018-11-30 12:00:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(yourDate);
//Use calender instead of date to getHours and minute, due to deprecated
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.get(Calendar.HOUR);
calendar.get(Calendar.MINUTE);
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)+"."+calendar.get(Calendar.MINUTE)); //12.0

Related

I want my string to converted to a day [duplicate]

This question already has answers here:
How to determine day of week by passing specific date?
(28 answers)
Closed 7 years ago.
I have this string
String s = "29/04/2015"
And I want it to produce the name of that day in my language, which is Norwegian.
For example:
29/04/2015 is "Onsdag"
30/04/2015 is "Torsdag"
How can I do this?
String dateString = "29/04/2015";
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = dateFormat.parse(dateString);
SimpleDateFormat formatter = new SimpleDateFormat("E", Locale.no_NO);
String day = formatter.format(date);
Now day will have the day in given locale. Update
You need to configure an instance of DateFormat, with your locale, (take a look at https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html).
then parse the Date and get the day, as Dilip already suggests.
You can use date parsing combined with Locale settings to get the desired output. For e.g. refer following code.
String dateStr = "29/04/2015";
SimpleDateFormat dtf = new SimpleDateFormat("dd/MM/yyyy");
Date dt = dtf.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
String m = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG_FORMAT, new Locale("no", "NO"));
System.out.println(m);
For more information about locale, visit Oracle Java Documentation.
First you will need to parse the String to a Date. Then use a Calendar to get the day of the week. You can use an array to convert it to the appropriate string.
// Array of Days
final String[] DAYS = {
"søndag", "mandag", "tirsdag", "onsdag", "torsdag", "fredag", "lørdag"
};
// Parse the date
final String source = "27/04/2015";
final DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
try {
date = format.parse(source);
} catch (final ParseException e) {
e.printStackTrace();
}
// Convert to calendar
final Calendar c = Calendar.getInstance();
c.setTime(date);
final int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
// Get the day's name
System.out.println("Day of Week: " + dayOfWeek);
System.out.println("Day = " + DAYS[dayOfWeek - 1]);
You need to parse your text with date to Date instance and then format it back to text. You can do it with SimpleDateFormat class which supports many patterns of dates like
dd/MM/yyyy for your original date,
and EEEE for full name of day in month.
While formatting you will also need to specify locale you want to use. To create Norway specific locale you can use for instance
Locale nor = Locale.forLanguageTag("no-NO");
So your code can look more or less like:
String text = "29/04/2015";
Locale nor = Locale.forLanguageTag("no-NO");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", nor);
SimpleDateFormat dayOfWeek = new SimpleDateFormat("EEEE", nor);
Date date = sdf.parse(text);
System.out.println(dayOfWeek.format(date));
Output: onsdag.
final int SUNDAY = 1;
final int ONSDAG = 2;
final int TORSDAG = 3;
....
....
String s = "29/04/2015";
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = dateFormat.parse(s);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DAY_OF_WEEK);
String dayString;
switch (day) {
case(ONSDAG):
dayString = "ONSDAG";
break;
....
}
EDIT: I just tested this and it actually starts from Sunday, and returns the value of 1 for sunday, I've changed the constant values to reflect this.
First you'll need a Calendar object.
Calendar cal = Calendar.getInstance();
String s = "29/04/2015"
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
cal.setTime(format.parse(s));
From the Calendar you can get the day of the week.
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
dayOfWeek will be 1-7 with Sunday (in english) being 1
You can use an HashMap map where the first parametri is the date "29/4/2015" while the second is the meaning. You can use your string to get the meaning map.get (yourString).

android getDate from milliseconds stored in String field

I have a date stored in a String field in SQLITE with the String value
"/Date(1411472160000+0100)/"
how can I convert this back into a date format , the code below doesn't work. I think I need to convert from the milliseconds first but I cant see how to even get the above text into a long format first ?
any suggestions ?
Date convertedDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm",
java.util.Locale.getDefault());
convertedDate = dateFormat.parse(dateString);
return dateFormat.format(convertedDate);
Well, a substring from the indexOf("(") to the indexOf("+") and you should find the date in milli.
From there, I believe you can find the date ;)
String s = "/Date(1411472160000+0100)/";
s = s.substring(s.indexOf("(") + 1, s.indexOf("+"));
Date d = new Date(Long.parseLong(s));
With the same structure, you can find the timezone (+0100) (from "+" to ")") and work with a Calendar to find the right time for the right time area.
First you have to parse out the time value from String i.e. "1411472160000+0100" part.
Here in "1411472160000+0100" , "+0100" is the timezone info. If you don't want to consider the timezone, then you can take following approach.
Approach-1
long timestamp = 1245613885;
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.setTimeInMillis(timestamp * 1000);
int year = calendar.get(Calendar.YEAR);
int day = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
then to get the date in your specified format you can use-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(calendar.getTime());
System.out.println(dateString); // 2009-06-21 15:51:25
Besides this approach, there is an excellent Java Date library called JodaTime.
If you want to incorporate the timezone info , you can refer to this constructor from JodaTime.
http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#DateTime-long-org.joda.time.DateTimeZone-

How to format my date with SimpleDateFormat?

I'm trying to set a date format, but when i run this code
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
System.out.println("datefield = "+date);
i take result:
oldstring = 2013-01-1
datefield = Tue Jan 01 00:01:00 MSK 2013
Why datefield isn't equal 2013-01-1?
At first mm in yyyy-mm-dd mean minute not Month. to set month use MM.
It would be look like this :
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
UPDATE
Try this:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
String sdf = new SimpleDateFormat("yyyy-mm-dd").format(date);
System.out.println("datefield = "+sdf);
If you don't use new SimpleDateFormat("yyyy-mm-dd").format(date);
you getting standard date format which include all info. If you want special format you need to use
new SimpleDateFormat("yyyy-mm-dd").format(date);
Also read this article about date formatting
The type of datefield is Date, so the toString method will basically always return the same format, as you are not overriding it.
So what you need to do, is basically:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(oldstring);
System.out.println("datefield = "+date);
String outDateStr = sdf.format(date);
System.out.println("newstring = "+outDateStr);
Use MM for month. mm is for minutes

Incrementing date in yyyymmddhhmmss.mmm format in Java

I want to increment the milliseconds in any given date in the format yyyymmddhhmmss.mmm in each iteration. mmm here represents milliseconds. And I want to perfom this operation in Java 1.5.
For example: 20120823151034.567 should be incremented to 20120823151034.568
You can use long milli-seconds which make incrementing trivial.
final String input = "20120823151034.567";
final DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Date d = df.parse(input);
d.setTime(d.getTime()+1);
System.out.println(df.format(d));
I wouldn't use Calendar as its very slow.
You can parse String to Date object and use getTime() and setTime(long l) to modify date. Then you can convert Date object back to String. For parsing String and converting Date object back to String you can use SimpleDateFormat class.
The best class to use for this operation is Calendar. You set it to the desired date, and then use
myCalendar.add(Calendar.MILLISECOND, 1);
to advance it by one millisecond. Use DateFormat to produce string representations.
This gives you what you want. It will work across any day/month/year boundary, as well as handling the start and end of daylight saving time.
final String input = "20120823151034.567";
final DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
final Calendar c = Calendar.getInstance();
c.setTime(df.parse(input));
c.add(Calendar.MILLISECOND, 1);
System.out.println(df.format(c.getTime()));
You can use Calendar Class as well as Date Class for this....
Date Class:
final String dateStr = "20120823151034.567";
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Date date = format.parse(input);
date.setTime(date.getTime()+1);
System.out.println(format.format(date));
Calendar Class:
final String dateStr = "20120823151034.567";
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(dateStr ));
c.add(Calendar.MILLISECOND,1);
System.out.println(format.format(c.getTime()));
In both cases, format.parse() has the potential to throw a ParseException, which you will need to catch and handle.
An alternative without using Calendar (although, Calendar is fine)
final String input = "20120814120000.111";
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Date date = new format.parse(input);
long time = date.getTime();
Date incrementedDate = new Date(time + 1);
System.out.println(format.format(date));

How to format a date String into desirable Date format

I was trying to format a string into date.
For this I have written a code:-
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdf.format( cal.getTime() ));
This is fine..
But now I want to convert a string into a date formatted like above..
For example
String dt="2010-10-22";
And the output should be like this:-
2010-10-22T00:00:00
How do I do this?
String dt = "2010-10-22";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition ps = new ParsePosition(0)
Date date = sdfIn.parse(dt, pos)
SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdfOut.format( date ));
This should do it for you, remember to wrap it in a try-catch block just in case.
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try
{
Date today = dt.parse("2010-10-22T00:00:00");
System.out.println("Your Date = " + dt.format(today));
} catch (ParseException e)
{
//This parse operation may not be successful, in which case you should handle the ParseException that gets thrown.
//Black Magic Goes Here
}
If your input is going to be ISO, you could also look at using the Joda Time API, like so:
LocalDateTime localDateTime = new LocalDateTime("2010-10-22");
System.out.println("Formatted time: " + localDateTime.toString());
The same class you use for output formatting of dates can also be used to parse dates on input.
SimpleDateFormat reference
To use your example, to parse the sample date:
String dt = "2010-10-22";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormatter.parse(dt));
The fields that are not specified (ie. hour, minutes, etc) will be 0. So your same code can be used to format the date on output.
Date Format Example
Containing the Conversion of String Date object from one format to another

Categories

Resources