Example usage for org.joda.time.format DateTimeFormat forPattern

List of usage examples for org.joda.time.format DateTimeFormat forPattern

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormat forPattern.

Prototype

public static DateTimeFormatter forPattern(String pattern) 

Source Link

Document

Factory to create a formatter from a pattern string.

Usage

From source file:com.jay.pea.mhealthapp2.safetyMonitor.SafetyMonitor.java

License:Open Source License

/**
 * Independent Safety Monitor checks the number of doses taken per day for a medication and assures it does not exceed the required number of doses (frequency). Function must account for dismissed alarms.
 *
 * @return//ww  w  .j av  a2s.  co  m
 */
public boolean checkDoseFreq() {
    //create a dose List to match the main dose list
    ArrayList<Dose> doseList = new ArrayList<>();
    if (medList.isEmpty())
        return true;
    //get all doses and add to doseList
    for (Medication med : medList) {
        HashMap<DateTime, DateTime> doseMap1 = med.getDoseMap1();
        HashMap<DateTime, Integer> doseMap2 = med.getDoseMap2();
        for (DateTime doseDate : doseMap1.keySet()) {
            //get takenDate and alertOn
            DateTime takenDate = doseMap1.get(doseDate);
            int alertOn = doseMap2.get(doseDate);
            Dose dose = new Dose(med, doseDate, takenDate, alertOn);
            doseList.add(dose);
        }
    }
    DateTime today = new DateTime().now();
    todaysDoseList = new ArrayList<>();
    //get today's doses
    for (Dose dose : doseList) {
        DateTimeFormatter dayFormat = DateTimeFormat.forPattern("dd MMM yyyy");
        if (dose.getDoseTime().toString(dayFormat).equals(today.toString(dayFormat))) {
            todaysDoseList.add(dose);
        }
        //sort collect, add to adaptor
        Collections.sort(todaysDoseList, new DoseComparator());
    }
    for (Dose d : todaysDoseList) {
        for (Medication safeMed : safeMedList) {
            if (safeMed.getMedName().equals(d.getMedication().getMedName())
                    & safeMed.getMedStart() == (d.getMedication().getMedStart())) {
                if (d.getMedication().getFreq() == safeMed.getFreq()) {
                    return true;
                }
            }
        }
    }
    // safety fail as have not matched frequency
    return false;
}

From source file:com.jbirdvegas.mgerrit.adapters.ChangeListAdapter.java

License:Apache License

@Override
public String categoryName(int position) {
    Cursor c = (Cursor) getItem(position);
    Integer index = getDateColumnIndex(c);
    // Convert to date
    DateTime date = Tools.parseDate(c.getString(index), mServerTimeZone, mLocalTimeZone);
    return DateTimeFormat.forPattern("MMMM dd, yyyy").withLocale(mLocale).print(date);
}

From source file:com.jsmartframework.web.tag.FormatTagHandler.java

License:Open Source License

String formatValue(final Object value) {
    if (value != null) {
        if (Type.NUMBER.equalsIgnoreCase(type)) {
            return new DecimalFormat(regex).format(value);

        } else if (Type.DATE.equalsIgnoreCase(type)) {
            if (value instanceof Date) {
                return new SimpleDateFormat(regex, getRequest().getLocale()).format(value);

            } else if (value instanceof LocalDateTime) {
                return ((LocalDateTime) value)
                        .format(DateTimeFormatter.ofPattern(regex).withLocale(getRequest().getLocale()));

            } else if (value instanceof DateTime) {
                return ((DateTime) value)
                        .toString(DateTimeFormat.forPattern(regex).withLocale(getRequest().getLocale()));
            }/*from  w w w . j  av  a 2s .c o m*/
        }
        return value.toString();
    }
    return null;
}

From source file:com.kana.connect.server.receiver.SMSKeywordDispatchReplyHandler.java

License:Apache License

/**
 * Helper method that transforms an SMPP Message into an XML document
 *///from  w  w w .jav  a2 s .c  om
public static String smppToXml(SmppReceiverMessage msg) {
    SMPPRequest smppReq = msg.getSmppRequest();

    StringBuffer buf = new StringBuffer();
    buf.append("<smpp>\n");

    // smpp header 
    // numeric values do not need to be escaped
    buf.append("<header>");
    buf.append("<command_id>").append(smppReq.getCommandId()).append("</command_id>");
    buf.append("<sequence_number>").append(smppReq.getSequenceNum()).append("</sequence_number>");
    buf.append("</header>\n");

    // smpp source
    Address smppSource = smppReq.getSource();
    buf.append("<source>");
    buf.append("<ton>").append(smppSource.getTON()).append("</ton>");
    buf.append("<npi>").append(smppSource.getNPI()).append("</npi>");

    String srcAddr = smppSource.getAddress();
    srcAddr = StringEscapeUtils.escapeXml11(srcAddr);
    buf.append("<address>").append(srcAddr).append("</address>");
    buf.append("</source>\n");

    // smpp dest
    Address smppDest = smppReq.getDestination();
    buf.append("<destination>");
    buf.append("<ton>").append(smppDest.getTON()).append("</ton>");
    buf.append("<npi>").append(smppDest.getNPI()).append("</npi>");

    String dstAddr = smppDest.getAddress();
    dstAddr = StringEscapeUtils.escapeXml11(dstAddr);
    buf.append("<address>").append(dstAddr).append("</address>");
    buf.append("</destination>\n");

    // message id
    String msgId = smppReq.getMessageId();
    if (msgId == null) {
        msgId = "";
    }
    msgId = StringEscapeUtils.escapeXml11(msgId);
    buf.append("<messageid>").append(msgId).append("</messageid>\n");

    // smpp message
    String msgText = smppReq.getMessageText();
    msgText = StringEscapeUtils.escapeXml11(msgText);
    buf.append("<message>");
    buf.append(msgText);
    buf.append("</message>\n");

    // base64 message
    msgText = smppReq.getMessageText();
    msgText = Base64.encodeBase64String(msgText.getBytes());
    buf.append("<messageBase64>");
    buf.append(msgText);
    buf.append("</messageBase64>\n");

    //
    // timestamps in different formats
    //

    long now = System.currentTimeMillis();

    // ISO8601 http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html
    DateTimeFormatter isoFormat = ISODateTimeFormat.dateTime();
    isoFormat.withZoneUTC();
    String headerTimestamp = isoFormat.print(now);
    headerTimestamp = StringEscapeUtils.escapeXml11(headerTimestamp);

    // custom format http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html
    DateTimeFormatter otherFormat = DateTimeFormat.forPattern("ddMMYYYYHHmmz");
    String payloadTimestamp = otherFormat.print(now);
    payloadTimestamp = StringEscapeUtils.escapeXml11(payloadTimestamp);

    buf.append("<headertimestamp>").append(headerTimestamp).append("</headertimestamp>");
    buf.append("<payloadtimestamp>").append(payloadTimestamp).append("</payloadtimestamp>");

    buf.append("</smpp>\n");
    return buf.toString();
}

From source file:com.knewton.mapreduce.example.StudentEventAbstractMapper.java

License:Apache License

/**
 * Sets up a DateTime interval for excluding student events. When start time is not set then it
 * defaults to the beginning of time. If end date is not specified then it defaults to
 * "the end of time".//from ww  w .j  ava2 s  .co m
 */
private void setupTimeRange(Configuration conf) {
    DateTimeFormatter dtf = DateTimeFormat.forPattern(DATE_TIME_STRING_FORMAT).withZoneUTC();
    String startDateStr = conf.get(PropertyConstants.START_DATE.txt);
    String endDateStr = conf.get(PropertyConstants.END_DATE.txt);
    // No need to instantiate timeRange.
    if (startDateStr == null && endDateStr == null) {
        return;
    }
    DateTime startDate;
    if (startDateStr != null) {
        startDate = dtf.parseDateTime(startDateStr);
    } else {
        startDate = new DateTime(0).year().withMinimumValue().withZone(DateTimeZone.UTC);
    }
    DateTime endDate;
    if (endDateStr != null) {
        endDate = dtf.parseDateTime(endDateStr);
    } else {
        endDate = new DateTime(0).withZone(DateTimeZone.UTC).year().withMaximumValue();
    }
    this.timeRange = new Interval(startDate, endDate);
}

From source file:com.knewton.mapreduce.StudentEventAbstractMapper.java

License:Apache License

/**
 * Sets up a DateTime interval for excluding student events. When start time is not set then it
 * defaults to the beginning of time. If end date is not specified then it defaults to
 * "the end of time"./*w  w w .java 2  s  . c om*/
 *
 * @param conf
 */
private void setupTimeRange(Configuration conf) {
    DateTimeFormatter dtf = DateTimeFormat.forPattern(DATE_TIME_STRING_FORMAT).withZoneUTC();
    String startDateStr = conf.get(START_DATE_PARAMETER_NAME);
    String endDateStr = conf.get(END_DATE_PARAMETER_NAME);
    // No need to instantiate timeRange.
    if (startDateStr == null && endDateStr == null) {
        return;
    }
    DateTime startDate;
    if (startDateStr != null) {
        startDate = dtf.parseDateTime(startDateStr);
    } else {
        startDate = new DateTime(Long.MIN_VALUE + ONE_DAY_IN_MILLIS).withZone(DateTimeZone.UTC);
    }
    DateTime endDate;
    if (endDateStr != null) {
        endDate = dtf.parseDateTime(endDateStr);
    } else {
        endDate = new DateTime(Long.MAX_VALUE - ONE_DAY_IN_MILLIS).withZone(DateTimeZone.UTC);
    }
    this.timeRange = new Interval(startDate, endDate);
}

From source file:com.kopysoft.chronos.activities.Editors.NoteEditor.java

License:Open Source License

@Override
public void onCreate(Bundle savedInstanceState) {
    if (enableLog)
        Log.d(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.note_editor);

    EditText editText = (EditText) findViewById(R.id.textField);
    TextView tv = (TextView) findViewById(R.id.date);

    DateTimeFormatter fmt = DateTimeFormat.forPattern("E, MMM d, yyyy");

    if (savedInstanceState != null) {
        date = savedInstanceState.getLong("date");
        Chronos chronos = new Chronos(this);
        gNote = chronos.getNoteByDay(new DateTime(date));
        chronos.close();/*  w ww  .  jav  a 2 s .  co m*/
        editText.setText(savedInstanceState.getString("data"));

    } else {
        date = getIntent().getExtras().getLong("date");
        Chronos chronos = new Chronos(this);
        gNote = chronos.getNoteByDay(new DateTime(date));
        chronos.close();
        editText.setText(gNote.getNote());
    }

    //tv.setText(fmt.print(new DateTime(date)));
    tv.setText(fmt.print(gNote.getTime()));

    Log.d(TAG, "Note Editor with Date: " + date);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //This is a workaround for http://b.android.com/15340 from http://stackoverflow.com/a/5852198/132047
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(R.drawable.bg_striped);
        bg.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        getSupportActionBar().setBackgroundDrawable(bg);

        BitmapDrawable bgSplit = (BitmapDrawable) getResources().getDrawable(R.drawable.bg_striped_split_img);
        bgSplit.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
    }
}

From source file:com.kopysoft.chronos.adapter.clock.PayPeriodAdapterList.java

License:Open Source License

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    RowElement curr = new RowElement(gContext, 10, 15, 15, 10);
    TextView left = curr.left();/*from  ww  w  . ja  va 2 s .c  om*/
    TextView right = curr.right();
    TextView center = curr.center();

    //Reset current view
    center.setText("");
    left.setText("");

    Duration dur = getTime(getItem(i), true);

    if (enableLog)
        Log.d(TAG, "Dur Total: " + dur.getMillis());

    DateTimeFormatter fmt = DateTimeFormat.forPattern("E, MMM d, yyyy");
    String time = fmt.print(gPunchesByDay.getDays().get(i));
    //String time = String.format("Hours %d:%02d ", dur.toPeriod().getHours(), dur.toPeriod().getMinutes());
    left.setText(time);
    //center.setTypeface(null, Typeface.ITALIC);

    if (dur.getMillis() >= 0) {
        time = String.format("%02d:%02d ", dur.toPeriod().getHours(), dur.toPeriod().getMinutes());
        right.setText(time);
    } else {
        right.setText("--:-- ");
    }

    return curr;
}

From source file:com.kopysoft.chronos.adapter.clock.TodayAdapterPair.java

License:Open Source License

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    if (view == null) {
        view = new RowElement(gContext);
    }// ww w . j  a v  a 2  s.  c om
    PunchPair pp = listOfPunchPairs.get(i);

    RowElement curr = (RowElement) view;
    TextView left = curr.left();
    TextView right = curr.right();
    TextView center = curr.center();

    //Set date format
    DateTimeFormatter fmt;
    if (!DateFormat.is24HourFormat(gContext))
        fmt = DateTimeFormat.forPattern("h:mm a");
    else
        fmt = DateTimeFormat.forPattern("HH:mm");

    //Set center text
    center.setText(pp.getInPunch().getTime().toString(fmt));

    //Set right text
    String rightString = "---     ";
    if (pp.getOutPunch() != null) {
        rightString = pp.getOutPunch().getTime().toString(fmt);
    }
    right.setText(rightString);

    //Set left text
    left.setText(pp.getTask().getName());

    return curr; //To change body of implemented methods use File | Settings | File Templates.
}

From source file:com.kopysoft.chronos.adapter.note.NoteAdapter.java

License:Open Source License

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    if (view == null) {
        view = new TwoLineElement(gContext);
    }/* w w  w.j  a v  a  2  s .  co m*/
    Note note = gListOfNotes.get(i);

    TwoLineElement curr = (TwoLineElement) view;

    TextView left = curr.left();
    TextView right = curr.right();
    TextView bottom = curr.secondRow();

    DateTimeFormatter fmt;
    if (!DateFormat.is24HourFormat(gContext))
        fmt = DateTimeFormat.forPattern("h:mm a");
    else
        fmt = DateTimeFormat.forPattern("HH:mm");

    left.setText(note.getTime().toString(fmt));

    //right.setText(note.getTask().getName());

    bottom.setText(note.getNote());

    return curr; //To change body of implemented methods use File | Settings | File Templates.
}