1

I am new to java and i am making a weather app.

I am getting data from openweathermap server in JSON format. Below is the format

 "sys":{  
      "type":1,
      "id":7758,
      "message":0.2194,
      "country":"India",
      "sunrise":1410569741,
      "sunset":1410614119
   },

I want to extract sunrise and sunset from it but what i want is only time and not date. Below is my code

DateFormat df = DateFormat.getDateTimeInstance();

            String sunrise= df.format(new Date(json.getJSONObject("sys").getLong("sunrise")*1000).getTime());
            String sunset = df.format(new Date(json.getJSONObject("sys").getLong("sunset")*1000).getTime());

Problem with my code is that it is giving me both time and date and output is " Sep 13, 2014 6:25:41 AM" for sunrise. Similar is for sunset. ?

How shall i extract "6:25:41 AM" it? I only want time and not year/month/day

1
  • Pleas search StackOverflow before posting. Commented Sep 15, 2014 at 0:35

2 Answers 2

3

You can do that by using the correct format: DateFormat.getTimeInstance().

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir, i found the answer using simpledateformat
2

try it this way

SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss a",Locale.ENGLISH);
System.out.println(df.format(new Date()));

Or this way

DateFormat ddf = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.ENGLISH);
System.out.println(ddf.format(new Date()));

Specify Locale it keeps you safe and Be carefull with the case of the letters for Example H means Hour of the day in 24-hour clock and h means hour of day in 12-hour clock. refer

3 Comments

Sorry for asking this late, in format "hh:mm:ss a", what does this "a" stands for??
it stands for Am/pm marker refer the link i gave you.
thank you very much sir, it solved all my doubts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.